3
votes

I have a 1 x n vector a and a 1 x m vector b. I want to create the n x m matrix whose j-th row is the vector a(j) * b. I have been doing this in MATLAB with

[M1, M2] = meshgrid(b, a);
M = M1.*M2

Since n, m are very large in my problem, I am looking for the most efficient way to make MATLAB do this, and I suspect my makeshift hack is not the answer. Thanks in advance for your help!

1

1 Answers

1
votes

Try

M = bsxfun(@times, a.', b);

On my computer, this is several times faster than your original code for vectors with thousands of elements.