Find index of all (non-unique) elements in a cell array as they appear in a second (sorted and unique) cell array

前端 未结 1 452
醉梦人生
醉梦人生 2020-12-06 19:57
A = {\'A\'; \'E\'; \'A\'; \'F\'};

B = {\'A\';\'B\';\'C\';\'D\';\'E\'; \'F\'};

I am trying to get for each string in cell array A, the

相关标签:
1条回答
  • 2020-12-06 20:37

    You flip the arguments to ismember, and you use the second output argument:

    [~,loc]=ismember(A,B)
    
    loc =
    
         1
         5
         1
         6
    

    The second output tells you where the elements of A are in B.

    If you are working with very strict limits to how many lines you can have in your code, and are in no position to fire the manager who imposed such limitations, you may want to access the second output of ismember directly. In order to do this, you can create the following helper function that allows to directly access the i-th output of a function

    function out = accessIthOutput(fun,ii)
    %ACCESSITHOUTPUT returns the i-th output variable of the function call fun
    %
    % define fun as anonymous function with no input, e.g.
    % @()ismember(A,B)
    % where A and B are defined in your workspace
    %
    % Using the above example, you'd access the second output argument
    % of ismember by calling
    % loc = accessIthOutput(@()ismember(A,B),2)
    
    
    %# get the output
    [output{1:ii}] = fun();
    
    %# return the i-th element
    out = output{ii};
    
    0 讨论(0)
提交回复
热议问题