more efficient alternative to repeated bsxfun?

2 ビュー (過去 30 日間)
Jeong Ho
Jeong Ho 2015 年 7 月 6 日
回答済み: Walter Roberson 2015 年 7 月 6 日
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 Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by