Question

I am working on a big matrix multiplication. I have a big matrix A (at least 5000x5000) and a column vector V (5000x1). In my code, each V is going to multiply each column of A element by element. I did it with a loop

K = zeros(5000, 5000);
for n=1:5000
  K(:, n) = V.*A(:, n);
end

but it is so slow. So I create a big matrix with each column assigned as V such that

MV=repmat(V,1,5000);
K = MV.*A;

it is fast but it waste too much of memory. When size of the matrix increase, it uses too much of memory. Is that any idea to use less memory but fast?

Was it helpful?

Solution

classic bsxfun

K = bsxfun( @times, A, V );

Alternatively, you might want to look at James Tursa's MTIMESX (found in FEX).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top