I have a matrix with 6330 rows and 4 columns (called part1) I wish to delete the rows containing one or more zeros. So for the following rows, row 1 and 3 should be deleted:
4 3 0 2
4 5 2 1
0 0 6 2
1 5 2 5
My guess is that there is 200-400 rows containing one or more zeros. I have tried with (also in loops)
part1(~any(part1,2),:) = [];
but it seem to only delete 15 rows (probably those where all the rows are zeros).
I am quite novel to MATLAB, so thanks for any help.

 採用された回答

KSSV
KSSV 2017 年 4 月 24 日

1 投票

A = [4 3 0 2
4 5 2 1
0 0 6 2
1 5 2 5] ;
iwant = A(all(A,2),:)

5 件のコメント

Jan
Jan 2017 年 4 月 24 日
Or:
part1(any(part1 == 0, 2), :) = []
KSSV
KSSV 2017 年 4 月 24 日
編集済み: KSSV 2017 年 4 月 24 日
Morten Thomsen commented:
Thanks for the quick answers. To my understanding, KSSVs suggestion is that it forms a new matrix (iwant) including only the rows without any zeros, while Jan Simons simply deletes the rows containing a zero in the old matrix.
However, I have tried them both (as well as in loops) and tried to mess about with them, but they do not seem to work (I examine size before and after). In fact, now no rows are deleted.
KSSV
KSSV 2017 年 4 月 24 日
A = [4 3 0 2
4 5 2 1
0 0 6 2
1 5 2 5] ;
size(A)
A = A(all(A,2),:) ;
size(A)
Jan
Jan 2017 年 4 月 24 日
@Morton Thomsen: We cannot guess what you are doing. Both codes do exactly what is asked for: Either create a new matrix without rows, which concern zeros, or delete rows, which concern zeros. Both outputs are identical.
If you code does something else, you are running different commands. Then please show them such that we can suggest improvements. Especially if you call such commands in a loop, there are several pitfalls.
Stephen23
Stephen23 2017 年 4 月 26 日
Morten Thomsen's "Answer" moved here:
I have messed around with it and works perfectly now. Thanks very much for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2017 年 4 月 24 日

コメント済み:

2017 年 4 月 26 日

Community Treasure Hunt

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

Start Hunting!

Translated by