我有两个单元格数组,
reference = [{'A'}, {'B'}, {'H'}, {'D'}, {'G'}];
array2 = [{'A'}, {'G'}, {'H'}, {'B'}, {'D'}];
对于单元格数组引用, matlab 中是否存在对array2进行排序的智能方法;
该函数应返回array2的索引,顺序与“reference”相同(array2将与引用相同)。
例如
magic_order = function(array2, reference)
....
end
magic_order(array2,reference)将返回
1,4,3,5,2
现在,如果我这样做
array2(1,4,3,5,2)
我会得到与参考
相同的内容排序功能
reference = [{'A'}, {'B'}, {'H'}, {'D'}, {'G'}];
array2 = [{'A'}, {'G'}, {'H'}, {'B'}, {'D'}];
[a, b] = sort(reference)
isequal(reference, array2(b))
ans = 0
答案 0 :(得分:4)
使用intersect
:
reference = [{'A'}, {'B'}, {'H'}, {'D'}, {'G'}];
array2 = [{'A'}, {'G'}, {'H'}, {'B'}, {'D'}];
[~, ~, idx] = intersect(reference, array2, 'stable');
返回:
>> isequal(reference, array2(idx))
ans =
1
使用setOrder='stable'
的intersect
(我已忽略)的第一个输出,返回reference
和array2
共有的值,顺序与reference
。第三个输出是第一个输出中的array2
索引。假设单元格数组相等,这将是array2
中reference
的索引。
答案 1 :(得分:3)
ismember
的第二个输出也可能有用。
reference = [{'A'}, {'B'}, {'H'}, {'D'}, {'G'}];
array2 = [{'A'}, {'G'}, {'H'}, {'B'}, {'D'}];
[~, pos] = ismember(reference,array2);
结果:
>> isequal(array2(pos),reference)
ans =
1