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

조회 수: 2 (최근 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에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by