Delete all rows in cell array based on value

I have a cell array A that contains multiple tables in the first column and add a vector (B) in the second column that shall indicate if a row is used or can be deleted.
A = {table(rand(3,2)); table(rand(3,2)); table(rand(3,2))};
B = {1; 0; 1};
C = horzcat(A,B);
I now have issues to delete all rows in cell array C that are indicated with a 0.
I have tried some proposed solutions for this kind of problem (deleting rows based on value) but could not find a working one for the specific problem.
Has someone an idea how to handle this issue?
Best regards!

 채택된 답변

J. Alex Lee
J. Alex Lee 2019년 12월 22일

1 개 추천

Do you need the cell array C? Must B be a cell array? If not...
A = {table(rand(3,2)); table(rand(3,2)); table(rand(3,2))};
% make B a logical array
% B = {1; 0; 1};
B = ([1;0;1]);
% don't need C
% C = horzcat(A,B);
A(~B) = [];
If for whatever reason you need an inline solution once you have the cell array C
A = {table(rand(3,2)); table(rand(3,2)); table(rand(3,2))};
B = {1; 0; 1};
C = horzcat(A,B);
C(~C[{:,2}],:)=[]

댓글 수: 9

Your solution is way better than mine! Thank you, I am going to save it. However it doesn't run on my Matlab, I changed it a bit:
A = {table(rand(3,2)); table(rand(3,2)); table(rand(3,2))};
B = [1; 0; 1];
C=A;
C(~B(:))=[];
Veronica, Alex's answer runs just fine - I actually tried it. It's how I would have done it if I wanted to change A in place.
If you wanted a separate output variable C, then you can do what you did but you don't need (:), so do it like this (which I also tested):
C = A; % Initialize.
C(~B) = [] % Remove elements where B is zero.
Could do help me to understand why it does not run on mine?
A = {table(rand(3,2)); table(rand(3,2)); table(rand(3,2))};
B = {1; 0; 1};
C = horzcat(A,B);
C(~C[{:,2}],:)=[]
C(~C[ {:,2} ],:)=[]
Error: Unbalanced or unexpected parenthesis or bracket.
I get this. I have Matlab R2017a. Thank you
Exactly what I was looking for! Thanks to both of you. You are correct, I don't need to write the logical array into the data cell array. I wanted to be sure that the issue was clear.
Veronica, C is a cell array and thus does not use square brackets for indexing. It only uses braces or parentheses. I think once you read the FAQ it should become clear to you. It will give you a good intuitive feel for what a cell array is and how to use it.
And horzcat() is not needed at all because there was never any need to stitch B onto the end of A.
Veronica Taurino
Veronica Taurino 2019년 12월 22일
편집: Veronica Taurino 2019년 12월 22일
I know this. Maybe there is a misunderstanding here. You said Alex's answer runs just fine, but I get that error using his solution (the second part of his answer). For this reason I was asking how it could run. I am not a native english speaker, there is a good chance my question is not clearly written, sorry about that. Thank you
It's my fault, I had a typo in the 2nd answer, sorry about that!
C(~[C{:,2}],:)=[]
This is just "undoing" the redundant cell-ification and horzcat of B onto A; just another way of saying what Image Analyst said above.
In more detail:
The FAQ above doesn't appear to succinctly cover what I was trying to do here. Multiple indexing of a cell array using curly braces returns multiple values, where as multiple indexing of a cell array using parantheses returns the subset of the cell array as another cell array
A = {table(rand(3,2)); table(rand(3,2)); table(rand(3,2))};
B = {1; 0; 1};
C = horzcat(A,B);
C(:,2)
returns
ans =
3×1 cell array
{[1]}
{[0]}
{[1]}
whereas
C{:,2}
returns
ans =
1
ans =
0
ans =
1
You can "catch" the multiple results of the latter by wrapping in square brackets, as if defining an array manually
[C{:,2}]
returns
ans =
1 0 1
Thank you Alex, I was going crazy moving those brackets around ahah neat solution, thank you, I am going to save it for later. Have a nice day
through browsing more on answers, I found this:
Actual documentation about what I was doing with [C{:,2}]

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Veronica Taurino
Veronica Taurino 2019년 12월 22일

2 개 추천

A = {table(rand(3,2)); table(rand(3,2)); table(rand(3,2))};
B = {1; 0; 1};
C = horzcat(A,B);
D={};
ii=0;
jj=0;
for ii=1: size(C,1)
if C{ii,2}==1
jj=jj+1;
D{jj}=C{ii,1};
D=D';
end
end
Something like that?

카테고리

도움말 센터File Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

질문:

2019년 12월 22일

댓글:

2019년 12월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by