how to delete the rest of the zeros from a matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
wasima tammi
am 6 Mär. 2015
Kommentiert: Sad Grad Student
am 24 Mär. 2015
suppose this the output c=[0 0 0 0; 0 0 0 0; 1 1 1 1] now how could i delete the other zeros for a looop and only print the last row??
1 Kommentar
Adam
am 6 Mär. 2015
Is that as complex as the problem gets or do you have case where some 1s appear in a row with 0s? If so are you expecting the output to simply be a 1d array of basically however many 1s exist in c, irrespective of the structure of where they appear?
Akzeptierte Antwort
Sad Grad Student
am 6 Mär. 2015
Bearbeitet: Sad Grad Student
am 6 Mär. 2015
To remove 0, you can simply do: c(~c) = []; you don't need a loop! Beauty of matlab :)
~c is essentially looking for empty values in c. In Matlab, 0 means empty value and you can simply remove it by replacing it with []
>> c=[0 0 0 0; 0 0 0 0; 1 1 1 1]
c =
0 0 0 0
0 0 0 0
1 1 1 1
>> c(~c) = []
c =
1 1 1 1
>>
2 Kommentare
Sad Grad Student
am 24 Mär. 2015
I need to know what exactly it is that you're doing. Post your code here in the comments so I can answer why you're getting unexpected number of columns.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!