필터 지우기
필터 지우기

Find first non-zero column of a matrix

조회 수: 6 (최근 30일)
Rodrigo Pereira
Rodrigo Pereira 2016년 8월 27일
편집: dpb 2016년 8월 27일
How do I, given a matriz A, tell MATLAB to find the index of the leftmost non-zero column of said matrix?

채택된 답변

dpb
dpb 2016년 8월 27일
편집: dpb 2016년 8월 27일
>> A=[zeros(5) randn(5)]; A=A(:,randperm(10)); % sample dataset
A =
-0.7440 1.0223 0 -0.7062 0 0.1257 0 1.2390 0 0
-0.3744 -1.4192 0 -1.0207 0 0.6963 0 0.8987 0 0
0.4241 2.5467 0 1.1340 0 1.7395 0 -1.1501 0 0
0.1141 -0.3580 0 0.0385 0 0.6654 0 -0.2642 0 0
-0.5111 0.4013 0 0.2309 0 0.6875 0 -1.6252 0 0
>> find(any(A),1) % first column nonzero
ans =
1
>>
That one seems too easy; let's try again...
>> A=A(:,randperm(10));
A =
0 -0.7440 0 0 1.0223 1.2390 0 0 0.1257 -0.7062
0 -0.3744 0 0 -1.4192 0.8987 0 0 0.6963 -1.0207
0 0.4241 0 0 2.5467 -1.1501 0 0 1.7395 1.1340
0 0.1141 0 0 -0.3580 -0.2642 0 0 0.6654 0.0385
0 -0.5111 0 0 0.4013 -1.6252 0 0 0.6875 0.2309
>> find(any(A),1)
ans =
2
>>
OK, that's more difficult and illustrates it does work as expected...

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 8월 27일
A=randi([0 2],20,8)
[~,out]=max(sum(logical(A)))

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by