2

I have a cell array, something like this:

A = 

    '5523'    '2012-10-26'    '23'    'T'    '17.7'
    '5513'    '2012-10-26'    '23'    'T'    '22.1'
    '5506'    '2012-10-26'    '23'    'C'    '16.2'

Now I would like to filter all records that have T. So I would like to get this array:

A = 

    '5523'    '2012-10-26'    '23'    'T'    '17.7'
    '5513'    '2012-10-26'    '23'    'T'    '22.1'

I could parse all array, but is there any other way?

1 Answer 1

6

Here's a one-liner to do it:

A = A(strcmp(A(:,4), 'T'), :);

The inner part, strcmp(A(:,4), 'T'), is comparing column 4 of all rows to 'T'. Then that boolean vector can extract matching rows from A with logical indexing.

Sign up to request clarification or add additional context in comments.

2 Comments

@llnk I know what you mean. But once you get used to logical indexing and matrix operations, "classic programming" seems very verbose and can be quite frustrating!
@llnk MATLAB is more oriented toward functional programming and favors vector operations. It looks very elegant once you get used to it.

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.