MATLAB: How to remove zeros from the matrix

matrixresize

Hello, I want to remove zero values from the matrix and cut the last elements of odd rows. For example, if I have a matrix
A=[1, 0, 2, 0, 3 ;
0, 4, 0, 5, 0 ;
6, 0, 7, 0, 8]
and, I want to make matrix like
B=[1, 2;
4, 5;
6, 7]
Please answer this question. Thanks!

Best Answer

I think this does what you want:
% Replicate A into B
B = A;
% For the even-numbered rows, circularly shift the elements
B(2:2:end,:) = circshift(B(2:2:end,:),-1,2);
% Remove any columnn with a zero
B(:,any(B==0)) = [];