Search code examples
arraysmatlabcell

How to find the index of a cell (vector case) in Matlab


I just search the following related discussions:

  1. How do I find a specific cell within a cell array?.

    1. https://www.mathworks.com/matlabcentral/answers/84242-find-in-a-cell-array

However, they are not what I want exactly.

v = [1 0]; u = [0 1];
C = {v, u; u, u+u}

I create a cell C above with each element a row vector.

If I do

C{2,2}

it shows

ans =

 0     2

Inversely, if I know [0 2], I want to find where it is, i.e., I want to get {2,2}, how could I do?

For the scalar case, the answer is shown in the second link; however, I cannot find the answer for the vector case so far.

Thanks!


Solution

  • Following this answer you linked to you can do :

    found = cellfun(@(c) isequal(c,[0 2]),c)
    

    which outputs

    
    found =
    
      2×2 logical array
    
       0   0
       0   1
    

    lastly to get the coordinates you would use find :

    [row,col] = find(found==1)
    

    The output will be

    row = 2
    col = 2