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
Dyuman Joshi
Dyuman Joshi 2023년 1월 26일
편집: Dyuman Joshi 2023년 1월 26일
@julian gaviria Just a note -
My and Stephen's code did exactly what was asked.
You should have clarified what exactly you wanted, in your question statement. The comments on my code clearly indicated what the code does, and you should have picked up from them that it is not what you want to do and mentioned it in reply to that.
Nevertheless, @Stephen23 has already provided an answer for your query.
I will be deleting my solution.

댓글을 달려면 로그인하십시오.

채택된 답변

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
Fangjun Jiang
Fangjun Jiang 2023년 1월 26일
Isn't it funny that my solution was correct after all?
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
Dyuman Joshi
Dyuman Joshi 2023년 1월 25일
This wouldn't work if there is a 1 after a 0
in = [1 1 0 1; 0 1 1 1; 0 0 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×4 logical array
1 0 0 1 0 1 0 0 0 0 0 0 0 0 1 0
Fangjun Jiang
Fangjun Jiang 2023년 1월 25일
You are right!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by