bsxfun to multiply matrix with vector multiple times in for loop --> how faster?
Show older comments
I have a 2D matrix of size NxN, and each row I am multiplying with a 1xN vector with bsxfun in a for loop like this:
for k=1:1000
b(k)=mean(bsxfun(@times,tb_filter2d,d(k,:)),2);
end
Is there a way to speed this up? d is of the size 1000xN.
Thank you!!
4 Comments
Adam
on 6 Mar 2017
Please be a little more precise with the exact sizes of each of your variables. You have b, d and tb_filter2d in that expression. What is N and which of these is your NxN matrix? and if N ~= 1000 how exactly does this work?
Stephen23
on 6 Mar 2017
Is b preallocated before the loop?
James Tursa
on 6 Mar 2017
If tb_filter2d is your NxN matrix, how does this even work since the result of the RHS is a vector and you are assigning it to a scalar element location b(k)?
Rik
on 6 Mar 2017
Is there a reason you can't use something like meshgrid to remove the loop?
Answers (2)
Ankita Nargundkar
on 9 Mar 2017
0 votes
1. If you are on MATLAB R2016b, you can use operators directly instead of bsxfun. Refer to this example
Something on similar lines, https://www.mathworks.com/matlabcentral/answers/210352-optimize-speed-up-a-big-and-slow-matrix-operation-with-addition-and-bsxfun
This shouldn't require more than a single vector-matrix multiplication,
result =(tb_filter2d/N)*d.';
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!