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!