Hi, I want to speed up my script with double loop.

1 回表示 (過去 30 日間)
ArxIv
ArxIv 2024 年 4 月 5 日
コメント済み: ArxIv 2024 年 4 月 6 日
I would like to eliminate double-loop that script below has.
I have difficulty in vectorizing/optimizing the code handling multi-dimensional arrays.
A is the sz×n matrix and B is the sz×m matrix. sz is the 1×p vector.
Thus, X shall be m×n×p matrix.
Can someone suggest me the
for i = 1:n
for j = 1:m
X(j,i,[1:length(sz)]) = A(:,i)' + B(:,j)';
end
end
I would be happy if some one suggest me solutions.

採用された回答

Stephen23
Stephen23 2024 年 4 月 5 日
編集済み: Stephen23 2024 年 4 月 5 日
X = permute(A,[3,2,1]) + permute(B,[2,3,1])
The data arrangement in memory is not optimal for that operation. Use BSXFUN for versions prior to R2016b.
  1 件のコメント
ArxIv
ArxIv 2024 年 4 月 6 日
Thank you so much for your advice!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by