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

3 Ansichten (letzte 30 Tage)
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?

Akzeptierte Antwort

Bruno Baptista
Bruno Baptista am 5 Dez. 2016
Bearbeitet: Bruno Baptista am 5 Dez. 2016
A = [matriz];
At = A';
N = length(A);
B = zeros(size(At));
idx = At == 0;
B(~sort(idx)) = At(~idx);
B=B'

Weitere Antworten (1)

Jacob Ziesenis
Jacob Ziesenis am 5 Dez. 2016
you can do this with the following line:
A(:,end)=0

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!

Translated by