more efficient alternative to repeated bsxfun?

3 visualizzazioni (ultimi 30 giorni)
Jeong Ho
Jeong Ho il 6 Lug 2015
Risposto: Walter Roberson il 6 Lug 2015
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

Risposte (1)

Walter Roberson
Walter Roberson il 6 Lug 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.)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by