MATLAB: Get the index of one cell of a cell array

cellcell arraycell arraysindexindexingMATLAB

From a given cell of a cell array, how is it possible to easily and quickly get its index ?
Thanks for help.

Best Answer

  • Suppose you have a cell array
    A = {1,2,3,4,5};
    and you extract a specific element of this array
    a = A(4);
    then, for a general case, you can find its index back using
    idx = find(cellfun(@(x) isequal(x,a{:}), A))
    if all the elements of A are scalar, then you can also write
    idx = find([A{:}]==a{1});