0

I have a 6 x 3 cell (called strat) where the first two columns contain text, the last column has either 1 or 2.

I want to take a subset of this cell array. Basically select only the rows where the last column has a 1 in it.

I tried the following,

ff = strat(strat(:, 3), 1:2) == 1;

The error message is,

Function 'subsindex' is not defined for values of class 'cell'.

How can I index into a cell array?

1
  • 2
    Try ff = strat([strat{:,3}]==1, 1:2); or ff = strat([strat{:,3}]==1, :); Commented Nov 25, 2014 at 12:03

1 Answer 1

1

Cell arrays are accessed through braces {} instead of parentheses (). Then, as a 2nd subtlety, when pulling values out of a cell arrays, you need to gather them...for numerics you gather them into regular arrays using [] and for strings you gather them into a new cell array using {}. Confusing, eh?

ff = { strat{ [strat{:,3}]==1 , 1:2 } };

Gathering into cell arrays this way can often give the wrong shape when you're done. So, you might try something like this

ind = find([strat{:,3}]==1);  %find the relevant indices
ff = {{strat{ind,1}; strat{ind,2}}';  %this will probably give you the right shape
Sign up to request clarification or add additional context in comments.

Comments

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.