MATLAB: FOR loop with matrices

for loopindexindexingmatrixvector

I am working on coding a problem where we have Matrix A (3×3) and vector x0 (3×1).
x(k+1) = A*x0 where k =0 this new x vector will then be multiplied to A
so x(1+1)= A*x1 and so on.
the only thing staying constant is A matrix with the vector x changing. But i want to store and keep each vector x.
I am having troubles writing this.

Best Answer

A = rand(3, 3);
x = rand(3, 1);
v = zeros(11, 3); % Pre-allocation!
v(1, :) = x;
for k = 1:10
v(k + 1, :) = A * v(k, :);
end
Then the vectors x_i are stores as columns of the matix v. Another approach with a cell:
v = cell(1, 11); % pre-allocation!
v{1} = x;
for k = 1:10
v{k + 1} = A * v{k};
end
Btw., look at James' comment again:
x1 = A * x0
x2 = A * x1 = A * A * x0 = A^2 * x0
x3 = A * x2 = ... = A^3 * x0
...