How to incorporate a conditional nonzero statement for multiple columns

Hey,
I have a matrix D and I would like to compare columns elements with each other with a conditional non-zero statement: if only one element in a row is > 0 then keep all zero values in that row 0 and make the nonzero value 1. In addition, if more than one element in a row is nonzero then make all elements in that row 0.
I really appreciate the help!
Many thanks!
Felix
A = [6;3;4];
B = [2;0;1];
C = [6;0;3];
D = [A,B,C]
D =
6 2 6
3 0 0
4 1 3
% desired result
E =
0 0 0
1 0 0
0 0 0

 採用された回答

Andrei Bobrov
Andrei Bobrov 2018 年 11 月 20 日

0 投票

D =[
6 2 6
3 0 0
4 1 3];
out = D > 0;
out(sum(out,2) > 1,:) = 0;

1 件のコメント

Felix Flores James
Felix Flores James 2018 年 11 月 21 日
編集済み: Felix Flores James 2018 年 11 月 21 日
Thank you Andrei
This worked perfectly!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by