Does Matlab improve the following:

I have a very long vector 1xr v and a very long vector w 1xs, and the matrix A rxs, which is sparse (but very large in size).

I expected Matlab to optimize as follows: I will not run into memory issues:

  A./(v'*w) 

but it looks like Matlab is actually trying to generate the full v'*w matrix, because I have a memory problem. Is there any way to overcome this? Note that there is no need to calculate all v'*w , because many values โ€‹โ€‹of A are 0 .

EDIT: if it were possible, one way to do this would be to do A(find(A))./(v'*w)(find(A));

but you cannot select a subset of the matrix ( v'*w in this case) without first calculating it and putting it in a variable.

+7
matrix matlab sparse-matrix
source share
1 answer
  • You can use bsxfun . This gives the same result as A./(v'*w) without generating the matrix v.'*w :

     bsxfun(@rdivide, bsxfun(@rdivide, A, v'), w) 
  • Another possibility: if you only need non-zero values, use:

     [ii jj Anz] = find(A); Anz./v(ii)'./w(jj).' 

    This gives the column vector corresponding to your A(find(A))./(v'*w)(find(A)) , again without generating v.'*w If you need a sparse matrix A./(v'*w) (instead, if the column vector is its non-zero values), use sparse(ii,jj,Anz./v(ii)'./w(jj).') .

+6
source share

All Articles