Find the position of the first non-zero value in a matrix

조회 수: 20 (최근 30일)
Giannis Nikolopoulos
Giannis Nikolopoulos 2021년 4월 30일
댓글: Giannis Nikolopoulos 2021년 5월 1일
Hello, I have the matrix of the image below and I want the position of the first non-zero value of the columns 2 until the end
i used the cumsum expression, it finds the first non-zero values:
out = mymatrix(cumsum(cumsum(mymatrix~=0)) == 1)
but when I do the find((mymatrix(cumsum(cumsum(mymatrix~=0)) == 1)) the result is not the desirable
Has someone any idea?
thanks in advance!

채택된 답변

KSSV
KSSV 2021년 4월 30일
A = rand(10) ;
idx = randperm(10*10,80);
A(idx) = 0 ;
iwant = zeros(1,10) ;
for i = 1:10
iwant(i) = find(A(:,i),1) ;
end
iwant
  댓글 수: 3
KSSV
KSSV 2021년 5월 1일
Hi yes you are right, sometimes it will not run. When there is no non-zero element in a column the positon will be empty and in that case it will trhrow werror.
I am creating a demo random data for shwoing the solution to problem. To keep zeros randomly, I have used randperm. You can apply the solution to your data striaghtaway.
Giannis Nikolopoulos
Giannis Nikolopoulos 2021년 5월 1일
Thanks for your answer!! finally worked!!
best

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by