more efficient alternative to repeated bsxfun?

2 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2015-7-6
[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.)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by