A = {'A'; 'E'; 'A'; 'F'};
B = {'A';'B';'C';'D';'E'; 'F'};
I am trying to get for each string in cell array A, the index that matches that string in cell array B. A will have repeated values, B will not.
find(ismember(B, A) == 1)
outputs
1
5
6
but I want to get
1
5
1
6
preferably in a one liner. I can’t use strcmp instead of ismember either as the vectors are different sizes.
The vectors will actually contain date strings, and I need the index not a logical index matrix, I’m interested in the number not to use it for indexing.
How do I do it?
You flip the arguments to
ismember, and you use the second output argument:The second output tells you where the elements of
Aare inB.If you are working with very strict limits to how many lines you can have in your code, and are in no position to fire the manager who imposed such limitations, you may want to access the second output of
ismemberdirectly. In order to do this, you can create the following helper function that allows to directly access the i-th output of a function