MATLAB: Making a matrix/vector using loop

columnsfor loopmatrixrowsvector

I am trying to make a matrixvector which can have particular number of rows and column with different columns or rows from the previous columns or rows using for loop. Can any one suggest some command to make this with an example if possible ?
Like i have a matrix 3*2 , i want to make it 3*3 by adding one more column with different element.
Similar for the vector as well.

Best Answer

M = rand(3,2); % 3x2 matrix
M_new = [M sum(M,2)]; % 3x3 matrix with 3rd column different from first two