1

I try to input matrix in matrix

A=[1 2;2 1];
C=[0 1];

and then input the matrix in new matrix with

D =[CA;CA^2;CA^3;........;CA^n]

i try to use

n=40;

a(1,1)=1;
a(1,2)=1;
a(2,1)=1;
a(2,2)=1;
C=[0,1];



for k=1:n
   for i=1:2
      for j=1:2
          d(i,j)=c*a(i,j)*^n
      end
   end
end

when n is integer but i can't do

how to solve it ?

thank you very much for your attention

5
  • "but i can't do" is NOT a question. Commented Nov 25, 2013 at 17:40
  • 1
    @Okuma.Scott maybe the question is ill-posed but there are some obvious misunderstandings of the basics of MATLAB syntax that need to be addressed. Commented Nov 25, 2013 at 17:49
  • 1
    @Falimond Sorry, I guess I'm becoming jaded. Good answer BTW. Commented Nov 25, 2013 at 18:02
  • @Falimond BTW it is best not to use i and j as variable names in Matlab. Commented Nov 26, 2013 at 11:36
  • @Shai Thanks for the suggestion. I've realized it in the past but rarely deal with complex quantities so it's become a bad habit. Commented Nov 26, 2013 at 18:51

2 Answers 2

2

Great answer by Falimond.
However, significant number of matrix multiplications can be saved here:
Instead of taking A to the power of k for each k, we only need to multiply by A the result of the previous iteration: D(k,:) = C*A^k = D(k-1,:)*A!

n = 10;
A = [1 2;2 1];
C = [0 1];
D = zeros(n, size(C,2) ); % pre-allocate always a good practice
D(1,:) = C*A; % init recursive process
for k=2:n
    D(k,:) = D(k-1,:)*A;
end 
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.