cut of zeros from a matrix based on the longest non-zero row
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Dave B
2021-11-22
编辑:Dave B
2021-11-22
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 个评论
Net Fre
2021-11-22
OK, much better than mine :)
Didn't know about any. Notice that your code will ingore non-zero negatives.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!