nik
nik

Reputation: 143

Search for a cell array within another cell array and display the index

I have a string array

sub_str = {'SN1','SN2'};
main_str = {'SN3','SN2','SN1','SN4'};

i would expect output (the index) for the sub_str in main string is [3 2]. Is there a one liner to this?

Upvotes: 0

Views: 46

Answers (1)

Divakar
Divakar

Reputation: 221514

Use second output argument from ismember -

Code

[~,ind] = ismember(sub_str,main_str)

Output

ind =
     3     2

You can also use intersect -

[~,~,ind] = intersect(sub_str,main_str)

Upvotes: 1

Related Questions