MATLAB: Removing zeros from matrix

nonzero element

Hi,
I have a matrix like this:
0 0 2 3 4 0;
1 0 2 0 0 1;
3 2 1 0 0 0;
0 1 0 3 0 1;
I want to remove the zeros so I could have something like this:
2 3 4;
1 2 1;
3 2 1;
1 3 1;
The number of zeros in each row of the original matrix is the same. Any idea how can I do this? Commands like A=A(A ~= 0) didn't work.
Thanks!

Best Answer

A = A.';
A = reshape(A(A~=0),3,4).';