Logical operation on column of a matrix
조회 수: 7 (최근 30일)
이전 댓글 표시
I'm dealing with a larger problem but say I have a 5x6 matrix: A=[0 0 0 0 1 0;0 0 0 1 0 0;1 0 0 0 0 1;0 0 1 1 1 1 ;0 1 0 1 1 0]
My goal is to complete the matrix such that for each column, as soon as 1 is present, then this will be 1 until the last row.
Typically:
Result=[0 0 0 0 1 0;0 0 0 1 1 0;1 0 0 1 1 1;1 0 1 1 1 1;1 1 1 1 1 1] Any clue ? I can find the index on columns no problem, but i dont know how to move forward then
댓글 수: 0
채택된 답변
Stephen23
2018년 8월 24일
편집: Stephen23
2018년 8월 24일
Here are a few methods to try:
>> A = [0,0,0,0,1,0;0,0,0,1,0,0;1,0,0,0,0,1;0,0,1,1,1,1;0,1,0,1,1,0]
A =
0 0 0 0 1 0
0 0 0 1 0 0
1 0 0 0 0 1
0 0 1 1 1 1
0 1 0 1 1 0
>> B = +(cumsum(A,1)>0)
B =
0 0 0 0 1 0
0 0 0 1 1 0
1 0 0 1 1 1
1 0 1 1 1 1
1 1 1 1 1 1
>> B = sign(cumsum(A,1))
B =
0 0 0 0 1 0
0 0 0 1 1 0
1 0 0 1 1 1
1 0 1 1 1 1
1 1 1 1 1 1
>> B = +~~cumsum(A,1)
B =
0 0 0 0 1 0
0 0 0 1 1 0
1 0 0 1 1 1
1 0 1 1 1 1
1 1 1 1 1 1
댓글 수: 0
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!