我試圖在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循環它花費更少的時間。我可以對此有一些看法嗎?