How can i find specified numbers in an array without using for loop?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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?
채택된 답변
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 Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!