フィルターのクリア

Trying to replace for loop with faster code

1 回表示 (過去 30 日間)
Lukas Netzer
Lukas Netzer 2021 年 8 月 23 日
コメント済み: Lukas Netzer 2021 年 8 月 23 日
I have the following code in a rather large script, which unfortunately is VERY slow:
for x = 1:size(table1)
for y = 1:size(Loc_all)
if ismember(table1.Loc(x),Loc_all(y)) == 1
table1.dur(x) = t_avg(y);
break
end
end
end
It definitely works, but again the duration time is not "workable".
So I tried the following:
f = ismember(table1.Loc,Loc_all) == 1;
[IndexM, IndexN]=find(f);
table1.dur(IndexM) = t_avg(IndexN);
It works for IndexM which defines the location in table1.Loc - but it wont work for IndexN - all I get here is 1's. What am I missing?
As you can see t_avg and Loc_all have the same size, as do table1.Loc and table1.dur - I'm expecting IndexN to give the Location in Loc_all where a match happens.
Loc_all only has unique values. table1.Loc is a large table of values which may or may not be in Loc_all.
Any hints are very much appreciated!
Thank you!
  1 件のコメント
DGM
DGM 2021 年 8 月 23 日
It would help to provide some succinct example data to demonstrate what exactly you're dealing with.

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

採用された回答

Voss
Voss 2021 年 8 月 23 日
The second output argument of the find function is column indices. In order to get indices in Loc_all where a match happens, you can use the second output argument of ismember:
[f, IndexN] = ismember(table1.Loc,Loc_all); % f is the same as your f; IndexN is what you expect, except it has zeros where no match occurred (i.e., where f is false)
IndexN = IndexN(f); % keep only the IndexN where a match occurred
IndexM = find(f); % convert logical index f to integer index IndexM; IndexM is the same as your IndexM
  1 件のコメント
Lukas Netzer
Lukas Netzer 2021 年 8 月 23 日
Thank you - looks so easy. Have a nice day/evening :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgrammatic Model Editing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by