If the row values in multiple columns = 1, set another column's row value to 1, else set to 0

1 回表示 (過去 30 日間)
I have a table in which multiple columns contain either 0s or 1s. Lets say the columns are called A, B, & C.
I need to add a column, D, which has a value of 1 if within that row, A, B & C all have values of 1. If not, then D should have a value of 0 for that row.
Ive attached a .mat file with columns A, B & C for an example, but I need the addiitonal column 'D' added to the table:
A B C D
0 1 1 0
0 1 1 0
1 1 1 1
0 0 1 0
1 1 1 1
1 1 0 0
0 0 0 0
1 1 1 1
0 0 0 0
1 0 0 0
% I tried doing something like this but didnt work and seems inefficient:
for i = 1:height(X)
if X(i,'A') == 1 && X(i,'B') == 1 && X(i,'C') == 1
D = [D 1];
end
end

採用された回答

Matt J
Matt J 2020 年 9 月 9 日
編集済み: Matt J 2020 年 9 月 9 日
D=all( X{:,{'A','B','C'}} , 2);
X=[X,array2table(D)]
  3 件のコメント
Matt J
Matt J 2020 年 9 月 9 日
I updated my answer accordingly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by