Cell Array Filtering Not Working

1 view (last 30 days)
Hello,
I have an issue filtering a cell matrix that is created from a standard array. When I create the cell matrix outright (not converting a standard matrix), my filtering works exactly as intended. For example, when I try to filter a cell matrix created by the following method:
K={1 32 5 6
4 12 6 8
09 3 6 1}
And filter the third column for each row containing a 6 via:
p=K([K{:,3}]==6,:)
I get exactly what I want which are the rows that correspond to the filtered third column of the original K cell matrix. When I create a cell matrix from a standard matrix though and try to filter the same way I get an error. For example, this code:
A=[1 34 2; 6 4 2; 9 1 0]
B={A}
C=B([B{:,3}]==2,:)
Gives the following error which corresponds to the line defining C:
Index exceeds matrix dimensions.
I don't understand what the difference between my first and second filtering methods are. How would I filter the cell matrix B exactly like I filtered K?
Thanks in advance.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 7 Jul 2015
Edited: Azzi Abdelmalek on 7 Jul 2015
A=[1 34 2; 6 4 2; 9 1 0]
B=num2cell(A)
C=B([B{:,3}]==2,:)
If you write B={A}, B is a 1x1 cell array. I think what you are looking for is num2cell(A) which is 3x3 cell array
  1 Comment
Patrick Comiskey
Patrick Comiskey on 7 Jul 2015
This works perfectly! Thank you for your quick response Azzi.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!