identifying entry elements in rows of logical matrix

1 回表示 (過去 30 日間)
julian gaviria
julian gaviria 2023 年 1 月 25 日
コメント済み: Dyuman Joshi 2023 年 1 月 27 日
I want to identifiy the entry elements (i.e., the first "1" value) in the rows of the following logical matrix
input_matrix = [1 1 1; 0 1 1; 0 0 0; 0 1 0];
the expected matrix would be the following:
output_matrix = [1 0 0; 0 1 0; 0 0 0; 0 1 0];
The position of the "entries" (first "1" values) in both input and output matrices is respected. Also, Further "1" values (following the entries) have been replaced with "0" values.
Thanks in advance for any hint.
  2 件のコメント
julian gaviria
julian gaviria 2023 年 1 月 26 日
Thank you @Dyuman Joshi

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

採用された回答

Stephen23
Stephen23 2023 年 1 月 25 日
編集済み: Stephen23 2023 年 1 月 26 日
A = [1,1,1;0,1,1;0,0,0;0,1,0]
A = 4×3
1 1 1 0 1 1 0 0 0 0 1 0
B = A .* (cumsum(A,2)==1) % replace .* with & to get a logical output
B = 4×3
1 0 0 0 1 0 0 0 0 0 1 0
  11 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 1 月 27 日
MATLAB works in mysterious ways XD

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

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2023 年 1 月 25 日
in = [1 1 1; 0 1 1; 0 0 0; 0 1 0];
M=size(in,1);
temp=[zeros(M,1), in];
d=diff(temp,1,2);
out=(d==1)
out = 4×3 logical array
1 0 0 0 1 0 0 0 0 0 1 0
  3 件のコメント
Fangjun Jiang
Fangjun Jiang 2023 年 1 月 25 日
You are right!

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

カテゴリ

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

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by