Delete all rows in cell array based on value

6 ビュー (過去 30 日間)
Jan Kielmann
Jan Kielmann 2019 年 12 月 22 日
コメント済み: J. Alex Lee 2019 年 12 月 24 日
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 日
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 件のコメント
Veronica Taurino
Veronica Taurino 2019 年 12 月 22 日
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
J. Alex Lee
J. Alex Lee 2019 年 12 月 24 日
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 日
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?

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by