MATLAB Answers

0

Find element index of one array which is equal to the values from another array

Asked by kaiqiang huang on 22 Nov 2019 at 11:03
Latest activity Commented on by ME
on 22 Nov 2019 at 11:48
Accepted Answer by ME
Given two arrays
A = [1,1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4];
B = [1,1,1,1,1,1,3,3,3,3];
return C=[1,2,3,4,5,6,11,12,13,14]
The first "3" in B is the 11th value in A.
The values in C is the index of the values in B which exist in A.
Any suggestions?

  0 Comments

Sign in to comment.

1 Answer

Answer by ME
on 22 Nov 2019 at 11:37
 Accepted Answer

A = [1,1,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,4];
B = [1,1,1,1,1,1,3,3,3,3];
u=unique(B);
C=[];
for i=1:numel(u)
C=[C find(A==u(i))];
end
A more elegant solution probably exists but this will work.

  2 Comments

Thank you so much! It works well !
No problem - happy to help!

Sign in to comment.