Boost logo

Ublas :

From: Gunter Winkler (guwi17_at_[hidden])
Date: 2005-04-18 06:21:05


On Monday 18 April 2005 12:30, Sorkin Dmitri wrote:
> Hi.
>
> I have two (sparse) matrices: 'A (NxM)', 'B(NxK)'.
> What is the good way to concatenate them
> horizontally into resulting matrix 'C (N x M+K)' ?

Maybe something like this works:

typedef some_sparse_type spm;

spm C(N,M+K, A.nnz()+B.nnz());

project( C, range(0,n), range(0,m) ) += A;
project( C, range(0,n), range(m,m+k) ) += B;

> The same question about vertical concatenation.
>
> (I am seeking for an equivalent for matlab
> 'C=[A,B]' and 'C=[A;B]' operations.)

But there is no "native" function that supports concatenation. Another way is
to write a loop by yourself and use C.push_back(i,j,value) to fill the new
Matrix row by row. Using push_back() is the fastest way to fill a sparse
matrix, but you have to assure that you insert the values in the correct
order.

mfg
Gunter

PS: If you create your own concat(A,B) routine, please, post it here.