How to remove rows with zeros in selected columns?
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Emma Kuttler
il 24 Feb 2022
Modificato: Scott MacKenzie
il 24 Feb 2022
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]
0 Commenti
Risposta accettata
Scott MacKenzie
il 24 Feb 2022
Modificato: Scott MacKenzie
il 24 Feb 2022
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 ]
zeroIdx = ~sum(~a(:,2:end) == 0,2);
a(zeroIdx,:) = []
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!