Iterative matrix multiplication in Matlab

215 Views Asked by At

I am newbie Matlab user and I have a basic question for one of you. I have two matrix P and Q (both of them have size 10x10). How can I write this operation in MATLAB?

$T_{1}=Q*P\\ T_{2}=T_{1}*P\\ T_{3}=T_{2}*P\\ \ldots\\ T_{n+1}=T_{n}*P$

In addition, I want to keep every matrix $T_{n}$ for every step. Thanks for helps!

2

There are 2 best solutions below

0
On BEST ANSWER

If you want to keep them, then you can do this:

Ts=cell(n+1,1);
Ts{1}=Q*P;
for i=2:(n+1)
  Ts{i}=Ts{i-1}*P;
end

Note that the curly braces are required for use of cell arrays.

2
On

Try the following:

% Indexes a three dimensional array.
T(:, :, 1) = Q*P;
for j = 2:n
    T(:, :, j) = T(:, :, j-1)*P
end