how to use for loop for iterations in matrices?

조회 수: 1 (최근 30일)
Tariq Ullah
Tariq Ullah 2021년 8월 20일
편집: Stephen23 2021년 8월 20일
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

채택된 답변

Stephen23
Stephen23 2021년 8월 20일
편집: Stephen23 2021년 8월 20일
"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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by