Matlab - Create N sparse matrices and sum them

251 views Asked by At

I have N kx1 sparse vectors and I need to multiply each of them by their transpose, creating N square matrices, which I then have to sum over. The desired output is a k by k matrix. I have tried doing this in a loop and using arrayfun, but both solutions are too slow. Perhaps one of you can come up with something faster. Below are specific details about the best solution I've come up with.

mdev_big is k by N sparse matrix, containing each of the N vectors.

fun_sigma_i = @(i) mdev_big(:,i)*mdev_big(:,i)';

sigma_i = arrayfun(fun_sigma_i,1:N,'UniformOutput',false);

sigma = sum(reshape(full([sigma_i{:}]),k,k,N),3);

The slow part of this process is making sigma_i full, but I cannot reshape it into a 3d array otherwise. I've also tried cat instead of reshape (slower), ndSparse instead of full (way slower), and making fun_sigma_i return a full matrix rather than a sparse one (slower).

Thanks for the help! ,

0

There are 0 answers