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.

2
  • 1
    possible duplicate of How to search for a string in cell array in MATLAB? Commented Oct 29, 2013 at 7: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. Commented Oct 29, 2013 at 7:11

1 Answer 1

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))
Sign up to request clarification or add additional context in comments.

3 Comments

More precisely, you'd want find(strcmp('y',cell_array)), so that you'd get the indices of the 'y' values.
Thanks, I'll clarify that you need find around strcmp.
+1 The second method is more general, as it doesn't require single-chracter strings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.