MATLAB: Removing columns from a matrix

matrixmatrix arraymatrix manipulation

Can anyone please how me how to remove columns from a matrix?
an example of what I am trying to do is remove the 4 columns of this 2×6 matrix:
[a,0,0,0,0,b; c,0,0,0,0,d]
to
[a,b;c,d]

Best Answer

One possibility:
[a,b,c,d] = deal(3,5,7,13); % Populate Variables
M = [a,0,0,0,0,b; c,0,0,0,0,d];
Mresult = reshape(M(M~=0), size(M,1), [])
Mresult =
3 5
7 13