cut of zeros from a matrix based on the longest non-zero row
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Nikolas Spiliopoulos
am 22 Nov. 2021
Kommentiert: Dave B
am 22 Nov. 2021
Hi all,
I want to cut all zeros from the right, based on "the longest non zero row". an example:
A=[ 1 2 0 0 0 0;3 4 5 0 0 0;5 6 0 0 0 0];
output: A=[ 1 2 0 ;3 4 5 ;5 6 0 ];
thanks in advance!
1 Kommentar
Akzeptierte Antwort
Dave B
am 22 Nov. 2021
Bearbeitet: Dave B
am 22 Nov. 2021
Another way to phrase this question is to say you want to remove columns from the right side of the matrix if the whole column is zeros:
A=[ 1 2 0 0 0 0;3 4 5 0 0 0;5 6 0 0 0 0];
lastnonzero=find(any(A~=0,1),1,'last') % the last column with a non-zero row
A(:,lastnonzero+1:end)=[]
3 Kommentare
Net Fre
am 22 Nov. 2021
OK, much better than mine :)
Didn't know about any. Notice that your code will ingore non-zero negatives.
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Matrix Indexing 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!