如何找到元素数组的索引数组? [英] How to find the index array of a array of elements?
本文介绍了如何找到元素数组的索引数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用一个例子来展示我想要的东西.
I'd like to use an example to show what I want.
A = [5 1 2 4 3]; % of distinct values
B = [3 3 1 5 2];
我可以找到在MATLAB中实现的库函数吗?
Can I find a library function implemented in MATLAB such that:
C = [5 5 2 3 1] = someFun(A, B)
即C(i) = find(A == B(i))
请注意,我要的是库函数,因为在这种情况下,它是高度优化的.如果您确定不存在这样的功能,那也是答案.
Note that I am asking for a library function, as in that case it is highly optimized. If you are sure there doesn't exist such a function, that is also an answer.
推荐答案
几个解决方案:
C = zeros(size(B));
for i=1:numel(B)
C(i) = find(A == B(i));
end
arrayfun
C = arrayfun(@(n)find(A==n), B)
使用BSXFUN进行向量化的相等性
[C,~] = find( bsxfun(@eq, B, A.') )
成员
[~,C] = ismember(B,A)
这篇关于如何找到元素数组的索引数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文