# identifying entry elements in rows of logical matrix

3 views (last 30 days)
julian gaviria on 25 Jan 2023
Commented: Dyuman Joshi on 27 Jan 2023
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 CommentsShowHide 1 older comment
julian gaviria on 26 Jan 2023
Thank you @Dyuman Joshi

Stephen23 on 25 Jan 2023
Edited: Stephen23 on 26 Jan 2023
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
Dyuman Joshi on 27 Jan 2023
MATLAB works in mysterious ways XD

Fangjun Jiang on 25 Jan 2023
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
Fangjun Jiang on 25 Jan 2023
You are right!

### Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!