how to use for loop for iterations in matrices?

1 view (last 30 days)
Hello, how can I use for loop for these operations?
let x1= rand(5,3);
and x2= rand(5,3);
also, A1=x2*pinv(x1);
Now, I want to use A1 and x2 to get X3, i.e
x3=A1*x2;
and then,
A2=x3*pinv(x2);
x4=A2*x3; and so on.
I want to get the final X matrix after 10 iterations using for loop. i.e x10.
Thank you

Accepted Answer

Stephen23
Stephen23 on 20 Aug 2021
Edited: Stephen23 on 20 Aug 2021
"how to use for loop for iterations in matrices?"
Do NOT number the variable names, unless you want to force yourself into writing slow, complex, inefficient code.
Use a cell array instead, with indexing, e.g.:
C = {rand(5,3),rand(5,3)};
for k = 3:10
A = C{k-1} * pinv(C{k-2});
C{k} = A * C{k-1};
end
M = C{10}
M = 5×3
0.0237 0.0329 0.0283 0.0242 0.0348 0.0300 0.0154 0.0228 0.0197 0.0195 0.0275 0.0236 0.0397 0.0558 0.0480

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!