## spInsertBtoAvect.m
function sA=spInsertBtoAvect(spA,IM,JN,spB)
#
# Inserted sparse matrix spB  into sparse matrix spA, 
# starting at row index IM and column index JN.
# When matrix spA and matrix spB elements overlaped 
# then they added together. 
#
##=========================================================================
## This Program is written by Andres Lahe,   2002-02-15
##                    e-mail: andres.lahe@ttu.ee
## LAST MODIFIED: Andres Lahe,   2009-01-03
## Copyright (c)  2002 by Tallinn University of Technology 
##                Department of Mechanics
##                http://www.ttu.ee/
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.

## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.

## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
## http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
##==========
#%%%%%%%%%%%%%%%%%
#if nargin != 4
if ~(nargin==4)
error(' function spInsertBtoAvect have wrong number of input arguments!') 
end
#
OnHore=issparse(spA);
if OnHore==0
   disp('!!!!!')
   spA  
   disp('!!!!!')  
   disp('The matrix spA(M,N) must be sparse!')
   error('The sparse matrix spA(M,N) must be instantiated!')   
## return
end
#%%%%%%%%%%%%%%%%%
#%reas, siis   iiB = iiB( : )'; jjB = jjB( : )' ;
#% lk.-l http://stackoverflow.com/questions/132092/what-is-your-favourite-matlab-octave-programming-trick on muid trikke ka
#%tulbas, siis   iiB = iiB( : ); jjB = jjB( : );
#%%%%%%%%%%%%%%%%%
#
IMB=IM-1;
JNB=JN-1;
#
[iiA, jjA, aaA]=find(spA);
 iiA = iiA( : ); 
 jjA = jjA( : );
 aaA = aaA( : );
[iiB, jjB, aaB]=find(spB);
 iiB = iiB( : ); 
 jjB = jjB( : );
 aaB = aaB( : );
iiB=iiB.+IMB;
jjB=jjB.+JNB;
 iiB = iiB( : ); 
 jjB = jjB( : );
iis=[iiA; iiB];
jjs=[jjA; jjB];
aas=[aaA; aaB];
#spconvert
Auus=sparse(iis, jjs, aas);
#  
sA=Auus;
#
endfunction
