Find elements meeting any of a number of criteria

后端 未结 4 1542
花落未央
花落未央 2020-12-19 05:27

I am trying to find the indices of elements in a vector that correspond to another vector, preferably without using loops. For example, my input might be:

DJ         


        
4条回答
  •  既然无缘
    2020-12-19 06:02

    For small datasets, it seems my original approach was faster than both the ismember solution proposed by Divakar and the intersect solution proposed by qmeeeeeee, but all three get beaten by Luis Mendo's solution using good old bsxfun. See below code, which times each approach:

    function somescript()
    
    IsmemberTime = timeit(@membersol)
    IntersectTime = timeit(@intersectsol)
    FindTime = timeit(@naivesol)
    BsxTime = timeit(@bsxfunsol)
    
        function membersol()
            rng(1)
            set = randi(30,[1000 15]);             % generate 1000 vectors of length 15, containing random integers
            for i=1:1000
                [~,out] = ismember(set(i,1:5),set(i,6:end));      % first 5 random integers are the values to be found in the remainder of the vector
            end
    
        end
    
        function intersectsol()
            rng(1)
            set = randi(30,[1000 15]);
            for i=1:1000
                [~,~,Output] = intersect(set(i,1:5),set(i,6:end));
            end
        end
    
        function naivesol()
            rng(1)
            set = randi(30,[1000 15]);
            for i=1:1000
                Output = find(ismember(set(i,6:end),set(i,1:5)));
            end
        end
    
        function bsxfunsol()
            rng(1)
            set = randi(30,[1000 15]);
            for i=1:1000
                [~, Output] = max(bsxfun(@eq, set(i,1:5).', set(i,6:end)), [], 1);
            end
        end
    end
    

    Which on my machine (running R2014b) returns the following timings:

    IsmemberTime =
    
        0.1101
    
    
    IntersectTime =
    
        0.2008
    
    
    FindTime =
    
        0.0698
    
    
    BsxTime =
    
        0.0218
    

    This suggests that, for small data sets at least, using find and ismember on the inverted order of vectors is actually faster than using ismember alone. Since there is also some fixed overhead for all methods from the generation of the datasets set that are used to test with, the difference seems to be pretty big. More thorough tests can ben found in the comments below.

提交回复
热议问题