MATLAB: Resize a matrix into another with different size

reshape

Hello, i got a matrix X and I want to transform X into a matrix rows x collumns, in this case into a 4 by 7 matrix. I used reshape but it doesnt work because of the 2 nans i must create i assume.
k = 26;
X = [1:k]';
[n,m] = size(X);
rows = 4;
columns = ceil(n/rows);
Into something like that, if possible:
x(1) x(5) x(9) x(13) x(17) x(21) x(25)
x(2) x(6) x(10) x(14) x(18) x(22) x(26)
x(3) x(7) x(11) x(15) x(19) x(23) NaN
x(4) x(8) x(12) x(16) x(20) x(24) NaN

Best Answer

extra = rows*ceil(numel(X)/rows) - numel(X);
reshape([X(:);nan(extra,1)],rows,columns)