Find the index of all (non-unique) elements in an array of cells, since they appear in a second (sorted and unique) array of cells

A = {'A'; 'E'; 'A'; 'F'};

B = {'A';'B';'C';'D';'E'; 'F'};

I am trying to get for each row an array of cells A, an index that matches that row in the array of cells B. Awill have duplicate values, Bwill not.

find(ismember(B, A) == 1)

exits

1
5
6 

but i want to get

1
5
1
6

preferably in one insert. I can not use strcmp instead of ismember or as vectors of different sizes.

The vectors will actually contain date strings, and I need an index, not a logical index matrix, I'm interested in a number that cannot be used for indexing.

How should I do it?

+5
source share
1 answer

ismember, :

[~,loc]=ismember(A,B)

loc =

     1
     5
     1
     6

, A B.

, , , , ismember . , i-

function out = accessIthOutput(fun,ii)
%ACCESSITHOUTPUT returns the i-th output variable of the function call fun
%
% define fun as anonymous function with no input, e.g.
% @()ismember(A,B)
% where A and B are defined in your workspace
%
% Using the above example, you'd access the second output argument
% of ismember by calling
% loc = accessIthOutput(@()ismember(A,B),2)


%# get the output
[output{1:ii}] = fun();

%# return the i-th element
out = output{ii};
+7

All Articles