필터 지우기
필터 지우기

How can i put all zeros in the end of the rows in a matrix?

조회 수: 6 (최근 30일)
Bruno Baptista
Bruno Baptista 2016년 12월 5일
편집: Bruno Baptista 2016년 12월 5일
i wanto to put all the zero elemets of a matrix to the end of the rows. For example if i have A=[1 0 2 3 0;1 3 0 2 5;0 2 4 0 1] i want it to become A=[1 2 3 0 0;1 3 2 5 0;2 4 1 0 0]
Like, A=[1 0 2 3 0 to A=[1 2 3 0 0
1 3 0 2 5 1 3 2 5 0
0 2 4 0 1] 2 4 1 0 0]
is there any way of doing this?

채택된 답변

Bruno Baptista
Bruno Baptista 2016년 12월 5일
편집: Bruno Baptista 2016년 12월 5일
A = [matriz];
At = A';
N = length(A);
B = zeros(size(At));
idx = At == 0;
B(~sort(idx)) = At(~idx);
B=B'

추가 답변 (1개)

Jacob Ziesenis
Jacob Ziesenis 2016년 12월 5일
you can do this with the following line:
A(:,end)=0

카테고리

Help CenterFile Exchange에서 Dynamic System Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by