Columns with at least one zero element

조회 수: 9 (최근 30일)
Anya
Anya 2014년 6월 16일
댓글: dpb 2014년 6월 16일
Hi,
If I have a matrix with random dimension mxn , how can I detect a column which have at least one zero element?
Thank you

채택된 답변

Mischa Kim
Mischa Kim 2014년 6월 16일
편집: Mischa Kim 2014년 6월 16일
Anya, you could use
A = [1 2 3 0 8; 5 0 1 2 2];
col = find(sum(A==0))
col =
2 4
col shows the columns which have at least one zero.
  댓글 수: 1
dpb
dpb 2014년 6월 16일
Just for comparison...
>> A = [1 2 3 0 8; 5 0 1 2 2];
>> (sum(A==0))
ans =
0 1 0 1 0
>> all(A)
ans =
1 0 1 0 1
>> ~all(A)
ans =
0 1 0 1 0
>>

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

추가 답변 (2개)

Jos (10584)
Jos (10584) 2014년 6월 16일
Let M be your mxm matrix:
tf = any(M==0,1) % true for columns with at least 1 zero
C = M(:,~tf) % columns with no zeros
  댓글 수: 2
Anya
Anya 2014년 6월 16일
This answer also works ! thx guys
dpb
dpb 2014년 6월 16일
NB:
any(M==0) --> identically equal to ~all(M). One rarely (if ever) needs to expressly test for zero.
See the doc for each for details...

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


dpb
dpb 2014년 6월 16일
idx=~all(M);

카테고리

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