【发布时间】:2017-09-13 14:41:07
【问题描述】:
我正在尝试在 MATLAB 中执行逐行“ismember”,以便找出Set 中Input 的每个元素的位置。
这是我到目前为止的工作。
function lia = ismemberrow(Input, Set)
lia = false(size(Input)); % Pre-assign the output lia
for J = 1 : size(Input,1)
% The J-th row of "lia" is the list of locations of the
% elements of Input(J,:) in Set
lia(J,:) = find(ismember(Set, Input(J,:)));
end
end
例如,如果变量Input和Set定义如下
Input = [1 4;
4 2;
4 3;
2 4;
1 2;
3 2];
Set = [3 2 4 1];
lia = ismemberrow(Input,Set) 的输出 lia 将是:
lia = [4 3;
3 2;
3 1;
2 3;
4 2;
1 2];
到目前为止,我的函数工作正常,但是这个函数在我的项目中被调用了很多次,所以我在想如果我可以减少 for 循环以减少它花费的时间。我可以对此发表一些意见吗?
【问题讨论】: