我有两个单元格数组,比如说:
a = {'ci' 'fox' 'a' 'd' 'fox' 'b' 'a'}
b = {'a' 'b' 'ci' 'd' 'e' 'fox'}
我需要找到b
中元素a
的索引
即我想得到这个结果:
result = [3 6 1 4 6 2 1]
我尝试使用INTERSECT
但没有成功
请注意,重复是可能的。
感谢。
答案 0 :(得分:0)
在你的result = [3 6 1 4 -1 2]
中你做错了包括出现' a'没有' b'?
假设您只在第一次出现的b成员中出现:
a = {'ci' 'fox' 'a' 'd' 'b' 'a'}
b = {'a' 'b' 'ci' 'd' 'e' 'fox'}
result = ones(1,length(a))*-1;
for ii=1:length(b)
tmp = find(strcmp(a,b(ii)));
if (tmp > 0)
result(ii) = tmp(1);
end
end
result
答案 1 :(得分:0)
如Dan所述,这与以下内容重复:
Find index of all (non-unique) elements in a cell array as they appear in a second (sorted and unique) cell array
(好)答案是:
[~,loc]=ismember(a,b)