MATLAB: Change matrix elements in a loop

forfor loopindexiteratematrixmatrix manipulation

I am trying to figure out how to use a for loop to change the elements in a matrix.
I want to do this ten times.
For example, if row 1 of a 6×3 matrix is (a,a,0), I want to make a loop that generates row 2 as (2a,2a,0). I am basically trying to multiply the initial row to make all 6 rows increase by a multiple of a.
Thank you in advance for any suggestions!

Best Answer

No loop necessary. Use bsxfun:
M = randi(9, 6, 3);
Mm = bsxfun(@times, M, [1:size(M,1)]')
This multiplies every row of ‘M’ by the column vector [1 2 ... 6]'.