0

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! ,

lennon310
  • 12,503
  • 11
  • 43
  • 61
  • 1
    What is the value of 'N' and 'k'? – Daniel Jan 10 '14 at 21:34
  • and how sparse are the vectors, i.e. how many non-zero elements? – A. Donda Jan 10 '14 at 22:28
  • Are you sure the the simple implementation is not also the most efficient in this case? `S = sparse(k, k); for i = 1 : N, S = S + mdev_big(:, i) * mdev_big(:,i)'; end`. No anonymous function (slow), no `full`, no 3D array. – A. Donda Jan 10 '14 at 22:34
  • In my working example, N is 1000 and K is 2500. Each vector has 500 non-zero elements. Looping is by far the slowest solution I've tried so far. – user3183461 Jan 13 '14 at 15:33

0 Answers0