Matlab equivalent of Numpy broadcasting?
Loops aren't bad in MATLAB anymore thanks to compiler optimizations like just-in-time acceleration (JITA). etc. Most of the time, I've noticed that a solution with loops in current MATLAB versions is much faster than complicated (albeit, cool :D) one-liners.
bsxfun
might do the trick but in my experience, it tends to have memory issues as well but less so than repmat
.
So the syntax would be:
AA = bsxfun(@minus,A,b)
where b
is the vector and A
is your big matrix
But I urge you to profile the loopy version and then decide! Most probably, due to memory constraints, you might not have a choice :)
The other answers are a bit out of date -- Matlab R2016b appears to have added broadcasting as a standard feature. An example from that blog post that matches the question:
>> A = ones(2) + [1 5]'A = 2 2 6 6
I don't know if this will speed up the code, but subtraction of a scalar from a vector doesn't have memory issues. Since your matrix size is so asymmetrical, the overhead from a for-loop on the short dimension is negligible.
So maybe
matout = matin;for j = 1:size(matin, 1) %3 in this case matout(j,:) = matin(j,:) - vec_to_subtract(j);end
of course, you could do this in place, but I didn't know if you wanted to preserve the original matrix.