Find the first incidence of 1 in a logical vector and replace all other 1s by zero

2 vues (au cours des 30 derniers jours)
I need to find the first incidence of 1 in a logical vector and replace all other 1s by zero.
For example, I have a logical vector like this: V = [ 0 1 0 0 1 0 1 ] and I need a function/code that will tell me that will yield V=[0 1 0 0 0 0 0]..
Since I am having a matrix (10000,35) size, how to run this code for each row?

Réponse acceptée

madhan ravi
madhan ravi le 23 Juil 2020
Wanted = (cumsum(matrix, 2) == 1) .* matrix
  2 commentaires
Walter Roberson
Walter Roberson le 23 Juil 2020
This answer is better than mine.
susman
susman le 23 Juil 2020
Thank you so much! I appreciate your help :)

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 23 Juil 2020
diff([zeros(size(YourMatrix,1),1),~cumprod(~YourMatrix,2)],[],2)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by