Access the array index using find

조회 수: 3 (최근 30일)
Pankaja Tanjore
Pankaja Tanjore 2015년 7월 2일
댓글: Titus Edelhofer 2015년 7월 2일
Hello ,
I have an array A1 = [1 12 23 45]; and
second array A1_1 = [45 23];
I need to find the index of 45 and 23 in first array A1. I am using following function to do this find(a==a_1(1)) --Output =4 find(a==a_1(2)) --Output =3 I want to get this for multiple elements of array .
It would be grateful if you let me know the changes that are to be done to get it for multiple elements in an array.
Looking forward to hear from you at the earliest.
Thanks Pankaja

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 2일
편집: Azzi Abdelmalek 2015년 7월 2일
a= [1 12 23 45];
b= [45 23]
out=arrayfun(@(x) find(a==x),b,'un',0)
Or using a for loop. The previous answer, also uses a loop.
for k=1:numel(b)
out{k}=find(a==b(k));
end

추가 답변 (1개)

Titus Edelhofer
Titus Edelhofer 2015년 7월 2일
Hi,
I guess you are looking for this:
A1 = [1 12 23 45];
A1_1 = [45 23];
[~,idx] = ismember(A1_1, A1)
idx =
4 3
Titus
  댓글 수: 2
Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 2일
What if A1 = [1 12 45 23 45]?
Titus Edelhofer
Titus Edelhofer 2015년 7월 2일
Yes, you are right. In this case you would find only the first of the two 45 ...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by