Filter löschen
Filter löschen

more efficient alternative to repeated bsxfun?

2 Ansichten (letzte 30 Tage)
Jeong Ho
Jeong Ho am 6 Jul. 2015
Beantwortet: Walter Roberson am 6 Jul. 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

Antworten (1)

Walter Roberson
Walter Roberson am 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.)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by