フィルターのクリア

How to vectorize a for loop with array multiplication?

2 ビュー (過去 30 日間)
Erin Pratt
Erin Pratt 2021 年 6 月 23 日
コメント済み: Erin Pratt 2021 年 6 月 23 日
Please help on replacing:
for i=1:Nm
Sloctemp(i,:,:)=Sloctemp(i,:,:)*Sort(m,i);
end
Where Nm = 19 Sloctemp is a 30x6x6 matrix, Sort is a 524288x19 matrix m=1
Sort is populated with only 0 or ones, so I am just trying to multiply Sloctemp(1:19,:,:) by either 1 or 0.
I tried:
i=1:Nm;
Sloctemp(i,:,:)=Sort(m,i).*Sloctemp(i,:,:);
but get the "Array dimensions must match for binary array op." error.
This section of the code gets run tens of millions of times, so I am just trying to get the fastes solution.
Thank you!

採用された回答

Jan
Jan 2021 年 6 月 23 日
編集済み: Jan 2021 年 6 月 23 日
Prefer to post the examples such, that they can be started by copy&paste.
Nm = 19;
Sloctemp = rand(30, 6, 6);
Sort = rand(524288, 19);
m = 1;
Result = Sloctemp;
Result(1:19, :, :) = Sloctemp(1:19, :, :) .* Sort(m, 1:19).';
% Compare with original version:
for i = 1:Nm
Sloctemp(i, :, :) = Sloctemp(i, :, :) * Sort(m, i);
end
max(abs(Result - Sloctemp), [], 'all')

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by