Columns with at least one zero element

8 ビュー (過去 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 日

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by