Can I vectorise or make this 'find' double-loop run faster?

6 ビュー (過去 30 日間)
J Yadav
J Yadav 2018 年 4 月 2 日
コメント済み: Stephen23 2018 年 4 月 3 日
Hi, Is there a possible way in which the formula that uses 'find' to update a matrix could do away with the double loops and make this calculation faster. Inputs:
1) Cmat - a 13x13 matrix,
2) Hist - a 1000x21 matrix of random numbers,
3) Chain - a 1000x22 matrix, such that Chain(:,1) contains numbers 1:13 and Chain(:,2:22) are zeros.
I run two loops to update the Chain matrix as follows:.
for i=2:size(Chain,2);
for k=1:size(Hist,1)
Chain(k,i) = find(Hist(k,i)< Cmat(Chain(k,i-1),:),1);
end
end
any suggestions are welcome.
Thanks for your inputs.
jayant

採用された回答

Matt J
Matt J 2018 年 4 月 2 日
編集済み: Matt J 2018 年 4 月 2 日
Assuming R2016b or later,
for i=2:size(Chain,2)
[~,Chain(:,i)] = max( Hist(:,i) < Cmat(Chain(:,i-1),:) ,[],2);
end
  2 件のコメント
J Yadav
J Yadav 2018 年 4 月 3 日
That's a marvellous reply. I don't understand the ~ notation, but the solution works and reduced my calculation time by > 90%. Thank you very much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by