我试图在MATLAB中执行一个行方向的“ismember”,以便找出Set
中Input
的每个元素的位置。 这是我迄今的工作。Row-wise“ismember”without for-loop
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循环它花费更少的时间。我可以对此有一些看法吗?