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

1 次查看(过去 30 天)
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?

采纳的回答

madhan ravi
madhan ravi 2020-7-23
Wanted = (cumsum(matrix, 2) == 1) .* matrix

更多回答(1 个)

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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by