DSPRelated.com
Forums

Re: memory assignment for sparse matrix

Started by tunc...@gmail.com December 18, 2009
hi
C=spalloc(m*n, (m+n)*p); may be helpful

>
>
>
> Hello All,
>
>I have a huge sparse matrix (10000000 X 105000), with
>about 10% non-zero elements. The position of these
>non-zero elements is known, but their values needs to
>be individually updated during iterations. I use
>for-loop to assign the values for these non-zero
>lements, but it takes almost 20 minutes for this
>process alone, not to mention iterations. The
>following is the code I wrote to assign the values:
>
>
>m 00; nP00; p;
>
>C=sparse(m*n, (m+n)*p);
>
>for i=1:m, for j=1:n, for h=1:p,
>
>%Put A(h,j) into the position of row (i-1)*n+j and
>column (h-1)*n+j;
>
>C((i-1)*n+j,(h-1)*n+j) = A(i,h); % A is 2000 x 15
>data matrix;
>
>%Put B(h,j) into the position of row (i-1)*n+j and
>column n*p+(i-1)*p+h;
>
>C((i-1)*n+j,n*p+(i-1)*p+h) = B(h,j); % B is a 15
>x 5000 data matrix;
>
>
>end, end, end;
>
>
>For a regular matrix, we usually use zeros(m, n) or
>ones(m, n) to initialize a memory block for the
>matrix, and doing so can signicantly increasing the
>speed of assigning individal values by using for-loop.
>For comparision, see the following examples:
>
>No pre-assignment of memory block:
>
>>clear a; tic,for i=1:500, for jP0, a(i,j)=i*j; end;
>end; toc
>>elapsed_time = 1.9380
>
>With pre-assignment of memeory block:
>> clear a ; a=zeros(500,500); tic,for i=1:500, for
>jP0, a(i,j)=i*j; end; end; toc
>> elapsed_time = 0.0160
>
>Unfortunately, for sparse matrix, using sparse(m,n)
>does not initialize a memory block, which cause the
>long running time when doing the assignment work.
>
>Can anybody give me a hand? I need to know how to
>assign a continueous memory block for a sparse matrix.
>
>
>
>Thanks in advance.
>
>
>
>Jianhang Lu
>
>
>
>
>
>
>__________________________________
>
>
>
>
>