Finding column index of the first instance of 1

조회 수: 4(최근 30일)
I have a logical array like this:
I want to extrat the colum index of the cells where the first instance of 1 is detected. Like this:
For example, you see here that the first two rows show 4, because that is where 1 is first detected.

채택된 답변

Walter Roberson
Walter Roberson 2022년 5월 2일
C = sum(cumprod(~X, 2),1) + 1;
C will be one more than the number of columns for any row that has no 1.
  댓글 수: 4
Walter Roberson
Walter Roberson 2022년 5월 3일
isempty(idx)

댓글을 달려면 로그인하십시오.

추가 답변(1개)

Jonas
Jonas 2022년 5월 2일
use the find() function together with a loop over each row
  댓글 수: 1
Pelajar UM
Pelajar UM 2022년 5월 2일
편집: Pelajar UM 2022년 5월 2일
Like this?
Doesn't work, because it doesn't find the first instance. It finds all the indices that meet this condition.
for p=1:n %n is the length of the logical array X
G(p,:)=find (X(p,:));
end

댓글을 달려면 로그인하십시오.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by