How can i find specified numbers in an array without using for loop?

1 回表示 (過去 30 日間)
metehan akinci
metehan akinci 2019 年 11 月 19 日
コメント済み: metehan akinci 2019 年 11 月 19 日
I want to find places of specified values in an defined array but without using for loop since it really slows down my code.
This is just an example of what i have done using for loop,
a=[1 2 3];
b{1}=[1 5 7 9];
b{2}=[-2 0 2 ];
b{3}=[-6 3 9 12 18];
for i=1:length(a)
answer(i) = find(ismember(b{i},a(i)));
end
I just want to use matlabs built in function or something else instead of using for loop.
  2 件のコメント
Image Analyst
Image Analyst 2019 年 11 月 19 日
Perhaps intersect()? I'm not sure what you're looking for since you didn't specify your desired output. What do you want as the output?
For loops are not always slow, especially for something that small. Slow for loops is a myth that has perpetuated far longer than it should. How large are your arrays?
metehan akinci
metehan akinci 2019 年 11 月 19 日
編集済み: metehan akinci 2019 年 11 月 19 日
I have approximately 5 million rows with 20 columns
Where 'a' is my data array. Each column of 'a' is constructed using some defined numbers which is the 'b' for this example but with different order. For example, First column of 'a' has a unique values that is stored on first row of cell 'b' and so on.
To be more clear, i have a cell that has unique values which is the 'b' for my example. Then i have a large data that consist of my unique values in different orders. I just want to find the order that corresponds to my unique values.
For this example,
Answer = [1 3 2];

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

採用された回答

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 11 月 19 日
solution:
answer=cell2mat(arrayfun(@(x,a) find(ismember(x{:},a)),b,a,'Uni',false))

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by