Logical operation on column of a matrix
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
cedric W
el 24 de Ag. de 2018
Respondida: cedric W
el 24 de Ag. de 2018
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 comentarios
Respuesta aceptada
Stephen23
el 24 de Ag. de 2018
Editada: Stephen23
el 24 de Ag. de 2018
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 comentarios
Más respuestas (1)
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!