First off, there is no need to declare the elements of A individually. As you show it in your first code snippet is fine.
So the main issue with this for loop (besides the fact that it doesn't actually mimic the process shown here D =[CA;CA^2;CA^3;........;CA^n]
) is that you are using the variable n
in d(i,j)=c*a(i,j)*^n
when in fact it is the variable k
that is being incremented by the first for
loop. So you are always computing d(i,j)=c*a(i,j)*^40
and should instead use k
in pace of n
.
a(i,j)*^k
is also incorrect syntax since *^
does not multiply nor does it exponentiate. MATLAB will return an error because of this.
Additionally, you will get Subscripted assignment dimension mismatch.
error because C is 1x2 matrix and A(i,j) is just one element.
The reason I said it won't mimic the process D =[CA;CA^2;CA^3;........;CA^n]
is because you are only doing element wise operations on C with A and then putting them into D, I'm fairly certain this is not your run of the mill matrix multiplication - even if you were to break it down correctly - but this is inefficient since MATLAB will do it for you.
clear D
n=10;
A=[1 2;2 1];
C=[0,1];
for k=1:n
D(k,:) = C*A^k;
end
D =
2 1
4 5
14 13
40 41
122 121
364 365
1094 1093
3280 3281
9842 9841
29524 29525