I have two cell arrays of string A and B. All cells of B are also in A. I want to find the index of cell of B in A. Thanks.
Example:
A=
'aaaa'
'bbbb'
'cccc'
'dddd'
'ffff'
B=
'ffff'
'aaaa'
ans=
5
1
or
ans=
1
5
use either intersect
or ismember
[~, idxInA] = intersect(A,B)
or
LocInA = find(ismember(A,B))
You can do this really simply, use the code below
indices = cell(size(B));
for i = 1:numel(B)
indices{i} = find(strcmpi(A,B(i)));
end
While I do recommend using ismember
or intersect
, those solutions will not handle case insensitive solutions. Also, those methods will not indicate how many times a specific index was matched, where my solution, will return all indices that match for each comparison.
UPDATE
Code I am running to test this.
A={'aaaa','bbbb','cccc','dddd','ffff','aaaa'};
B={'ffff','aaaa','cccc','qwerty'};
indices = cell(size(B));
for i = 1:numel(B)
indices{i} = find(strcmpi(A,B(i)));
end
indices
Which returns the following
indices =
[5] [1x2 double] [3] [1x0 double]
I do not see where you are having problems
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.