0

I have a 435x1 cell array whose elements are either 'y', 'n', or '?'. I want to find which indices are equal to 'y'.

With normal arrays, I just use the find function. But I can't use that with cell arrays because eq is not defined for type cell.

I think I can go through each element and do

for index=1:size(cell_array,1)
    if cell_array{index} == 'y'
        %add index to some array of indices
    end
end

But is there a vectorized way to go through the array and find indices contain elements that are equal to 'y'? Any help is appreciated.

Sterling
  • 3,835
  • 14
  • 48
  • 73
  • 1
    possible duplicate of [How to search for a string in cell array in MATLAB?](http://stackoverflow.com/questions/8061344/how-to-search-for-a-string-in-cell-array-in-matlab) – Eitan T Oct 29 '13 at 07:00
  • @EitanT - I think Sterling was most interested in the `[cell_array{:}]=='y'` bit, which was not a possible solution for the other question, so `find` could be used in the familiar way with `eq`. It's much the same functionally, I'll admit. – chappjc Oct 29 '13 at 07:11

1 Answers1

5

Since you know each cell will contain a single character you can concatenate all the cell elements and do a single vectorized test:

find([cell_array{:}]=='y')

Possibly the most straightforward way is just to use strcmp, which can accept a cell array as the second argument:

find(strcmp('y',cell_array))
chappjc
  • 30,359
  • 6
  • 75
  • 132