more efficient alternative to repeated bsxfun?
2 views (last 30 days)
Show older comments
Dear all,
Hi, I have
bsxfun(@times,bsxfun(@minus,A,B),C)
i.e., repeated bsxfun, where A is (N x L) matrix, B and C are (1 x L) matrices. I'm curious if there's a more efficient way to write it. I'd appreciate any and all opinions. Thank you very much in advance!
Best, John
0 Comments
Answers (1)
Walter Roberson
on 6 Jul 2015
[A, ones(size(A,1),1)] * [eye(length(B));-B] * diag(C);
is the mathematical equivalent. If you were doing this repeatedly with different A matrices then you could pre-calculate the second matrix product, leading to
D = [eye(length(B));-B] * diag(C);
[A, ones(size(A,1),1)] * D
However, keep in mind that reducing the number of obvious steps will not necessarily make the result any more efficient. Matrix multiplication of later matrices is done with an optimized algorithm that is approximately complexity n^2.38 (I think), but for smaller matrices it would be a slower n^3 algorithm. I think you will find that the bsxfun approach involves a lot fewer mathematical operations (but might have more function call overhead.)
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!