Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 index that matches that string in cell array B. A will have repeated values, B will not.

find(ismember(B, A) == 1)

outputs

1
5
6 

but I want to get

1
5
1
6

preferably in a one liner. I can't use strcmp instead of ismember either as the vectors are different sizes.

The vectors will actually contain date strings, and I need the index not a logical index matrix, I'm interested in the number not to use it for indexing.

How do I do it?

like image 551
Dan Avatar asked Jul 19 '12 13:07

Dan


People also ask

What does Ismember function do in Matlab?

Lia = ismember( A , B ) returns an array containing logical 1 ( true ) where the data in A is found in B . Elsewhere, the array contains logical 0 ( false ). If A and B are tables or timetables, then ismember returns a logical value for each row.

How do I get unique values of a Matlab cell?

C = unique( A , setOrder ) returns the unique values of A in a specific order. setOrder can be 'sorted' (default) or 'stable' . C = unique( A , occurrence ) specifies which indices to return in case of repeated values. occurrence can be 'first' (default) or 'last' .

How do you find the index of a value in Matlab?

In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.

How does find work in Matlab?

k = find( X ) returns a vector containing the linear indices of each nonzero element in array X . If X is a vector, then find returns a vector with the same orientation as X . If X is a multidimensional array, then find returns a column vector of the linear indices of the result.


1 Answers

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};
like image 101
Jonas Avatar answered Oct 03 '22 03:10

Jonas