Split cell based upon logical value

I have an n x 5 cell array, the 5th column is a logical (0 or 1).
I want to split this into two separate cells, one for false(0) and one for true (1), preserving the cell structure.
What's the best was of doing this?

Antworten (1)

Kaushik Lakshminarasimhan
Kaushik Lakshminarasimhan am 15 Feb. 2017
Bearbeitet: Stephen23 am 15 Feb. 2017

1 Stimme

You can treat your cell array like a matrix. So if X is your nx5 cell array,
truecells = X([X{:,5}],1:4);
falsecells = X(~[X{:,5}],1:4);

3 Kommentare

Stephen Thompson
Stephen Thompson am 15 Feb. 2017
Bearbeitet: Stephen23 am 15 Feb. 2017
Please clarify the use of X:
truecells = arbitrary([arbitrary{:,5}],1:4);
falsecells = arbitrary(~[arbitrary{:,5}],1:4);
This won't do anything. Thanks.
Sorry I'm not sure what you mean by "won't do anything". That line of code basically splits your original cell array into two cell arrays based on whether the 5th column was true or false.
Stephen23
Stephen23 am 15 Feb. 2017
Bearbeitet: Stephen23 am 15 Feb. 2017
@Stephen Thompson: based on the description in your question, Kaushik Lakshminarasimhan's answer fulfills your needs. What does "won't do anything" actually mean?
truecells = X([X{:,5}],:);
falsecells = X(~[X{:,5}],:);

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 15 Feb. 2017

Bearbeitet:

am 15 Feb. 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by