How to remove rows with zeros in selected columns?

1 回表示 (過去 30 日間)
Emma Kuttler
Emma Kuttler 2022 年 2 月 24 日
編集済み: Scott MacKenzie 2022 年 2 月 24 日
I have a matrix with 1363 rows and 21 columns. The first column is an identifier (1-21). I need to delete all rows that contain all zeroes with the exception of the first column. For example,
a = [ 1 0 5 0 6
2 0 0 0 0
3 3 5 9 12
4 0 0 0 0
5 6 9 1 0]
I want to produce b
b = [ 1 0 5 0 6
3 3 5 9 12
5 6 9 1 0]

採用された回答

Scott MacKenzie
Scott MacKenzie 2022 年 2 月 24 日
編集済み: Scott MacKenzie 2022 年 2 月 24 日
There is probably a simpler solution, but this will work...
a = [ 1 0 5 0 6
2 0 0 0 0
3 3 5 9 12
4 0 0 0 0 ]
a = 4×5
1 0 5 0 6 2 0 0 0 0 3 3 5 9 12 4 0 0 0 0
zeroIdx = ~sum(~a(:,2:end) == 0,2);
a(zeroIdx,:) = []
a = 2×5
1 0 5 0 6 3 3 5 9 12

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by