Find indexes in cell array

14 views (last 30 days)
Davide Bordin
Davide Bordin on 10 May 2022
Commented: Jon on 13 May 2022
Hi all,
i have 2 arrays one containing all my values (even duplicates) and one containing the unique elements.
My goal is to find the index of my values based on the index of the unique elements
R = [2 ,1 , 1 , 2 , 4 , [1,3] , [1,3] , 4 , [1,3] , [2,5] , [2,5] , 4]
C= [2 , 1 , 4 , [1,3] , [2,5]]
Vectors [1,3] should be treated as 1 unique element, not as 1 and 3
If its easier we can work with letters
R1=['2B' , '1A' , '1A' , '2B' , '1D' , '1A+1C' ,'1A+1C' , '1D' , '1A+1C' , '1B+1E' , '1B+1E', '1D']
C1=['1A' , '2B' , '1A+1C' , '1D' , '1B+1E']
In both cases i expect to get an array
idx = [2, 1, 1, 2, 4, 3, 3, 4, 3, 5, 5, 4]
The order is important, idx should be in the same order of the original R/R1 vector.
I tried find and a bit of arrayfun but couldnt get it to work
Thank you so much in advance

Accepted Answer

Jon
Jon on 10 May 2022
Edited: Jon on 10 May 2022
R1={'2B' , '1A' , '1A' , '2B' , '1D' , '1A+1C' ,'1A+1C' , '1D' , '1A+1C' , '1B+1E' , '1B+1E', '1D'}
C1={'1A' , '2B' , '1A+1C' , '1D' , '1B+1E'}
[~,Locb] = ismember(R1,C1)
  3 Comments
Jon
Jon on 13 May 2022
Glad that it helped, good luck with your project

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!