Row-wise "ismember" without for-loop

Viewed 251

I am trying to perform a row-wise "ismember" in MATLAB in order to find out where, in Set, each element of Input is. Here is my work so far.

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

For example, if the variables Input and Set are defined as follows

Input = [1 4; 
         4 2; 
         4 3; 
         2 4; 
         1 2; 
         3 2];
Set = [3 2 4 1];

The the output lia of lia = ismemberrow(Input,Set) will be:

lia = [4 3; 
       3 2; 
       3 1; 
       2 3; 
       4 2; 
       1 2];

My function works accurately so far, but this function is called many times in my project so I am thinking of that if I can reduce the for-loop so that it spends less time. May I have some opinions on it?

3 Answers
Related