How to set all elements in a 2D Matrix between two indices to "1" in each row
Afficher commentaires plus anciens
I've a matrix A e.g. like this:
A =
1 0 0 1 0 0
0 0 1 0 0 1
0 1 1 0 0 0
0 1 0 1 0 0
And I want all elements between the "ones" per row to be "one" as well. The result should look like this: B =
1 1 1 1 0 0
0 0 1 1 1 1
0 1 1 0 0 0
0 1 1 1 0 0
If it helps, I also have two vectors with the indices of the first and second one.
Thanks!!
PS: If possible the solution would not use any loops since the real matrix that I have to use is very large and the program shall be very efficient.
Réponse acceptée
Plus de réponses (2)
Andrei Bobrov
le 5 Déc 2013
cumsum(A,2)>0 & fliplr(cumsum(A(:,end:-1:1),2))>0
2 commentaires
Wayne King
le 5 Déc 2013
How you come up with some of these solutions Andrei! :)
Tim
le 6 Déc 2013
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!