Columns with at least one zero element

5 Ansichten (letzte 30 Tage)
Anya
Anya am 16 Jun. 2014
Kommentiert: dpb am 16 Jun. 2014
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

Akzeptierte Antwort

Mischa Kim
Mischa Kim am 16 Jun. 2014
Bearbeitet: Mischa Kim am 16 Jun. 2014
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 Kommentar
dpb
dpb am 16 Jun. 2014
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
>>

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jos (10584)
Jos (10584) am 16 Jun. 2014
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 Kommentare
Anya
Anya am 16 Jun. 2014
This answer also works ! thx guys
dpb
dpb am 16 Jun. 2014
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...

Melden Sie sich an, um zu kommentieren.


dpb
dpb am 16 Jun. 2014

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by