MATLAB: How to create a function of matrix with integer(from 1 to infinite) that can be arranged by row and column

functionloopmatrixrow

for example, if m=2, n=3 then the matrix become [1 2,3 4,5,6]' if m=4 n=4 it is [1 2 3 4,5 6 7 8,9 10 11 12,13 14 15 16]' I know I should use loop, but where should I start it? i think i may need some help here…it asks one output and two inputs.

Best Answer

Depending on the orientation you are after, either:
reshape(1:m*n,m,n)
or
reshape(1:m*n,n,m).'