4

I want to write this matrix in matlab,

s=[0  .....        0
   B    0   ....   0
   AB    B   ....  0
   .  .   .
   .     .    .
   .        .    . 0                  ....
   A^(n-1)*B ... AB    B ]

I have tried this below code but giving error,

N = 50;
A=[2 3;4 1];
B=[3 ;2];
[nx,ny] = size(A);

s(nx,ny,N) = 0;
for n=1:1:N
    s(:,:,n)=A.^n;
end
s_x=cat(3, eye(size(A)) ,s);

for ii=1:1:N-1
    su(:,:,ii)=(A.^ii).*B ;
end

z= zeros(1,60,1);
su1 = [z;su] ;
s_u=repmat(su1,N);

seems like the concatenation of matrix is not being done. I am a beginner so having serious troubles,please help.

Shai
  • 111,146
  • 38
  • 238
  • 371
tina066
  • 55
  • 4
  • what is `A^(n-1)` supposed to mean? `A*A*A...` or elementwise power? – m.s. May 12 '15 at 09:48
  • A to the power (N-1) every time N increasing +1 until 60 – tina066 May 12 '15 at 09:51
  • 1
    I think that your matrix description is wrong, can you write what matrix is, and also be sure of the operations for example for the operation (A.^ii).*B matlab give you this error (A.^2).*B Error using .* Matrix dimensions must agree. – anquegi May 12 '15 at 10:12
  • 1
    I think it's right, isn't it? If "0" is taken to indicate the zero matrix of appropriate size? I can't see a problem. The code is wrong, since it uses the element-wise power operation, but the matrix is fine. – Rattus Ex Machina May 12 '15 at 10:23

2 Answers2

1

Use cell arrays and the answer to your previous question

A = [2 3; 4 1];
B = [3 ;2 ];
N = 60;
[cs{1:(N+1),1:N}] = deal( zeros(size(B)) ); %// allocate space, setting top triangle to zero
%// work on diagonals
x = B;
for ii=2:(N+1)
    [cs{ii:(N+2):((N+1)*(N+2-ii))}] = deal(x); %//deal to diagonal
    x = A*x;
end 
s = cell2mat(cs); %// convert cells to a single matrix    

For more information you can read about deal and cell2mat.


Important note about the difference between matrix operations and element-wise operations

In your question (and in your previous one) you confuse between matrix power: A^2 and element-wise operation A.^2:

  • matrix power A^2 = [16 9;12 13] is the matrix product of A*A
  • element-wise power A.^2 takes each element separately and computes its square: A.^2 = [4 9; 16 1]

In yor question you ask about matrix product A*b, but the code you write is A.*b which is an element-by-element product. This gives you an error since the size of A and the size of b are not the same.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371
  • 1
    thank you for your time...i am trying hard to understand these codes rather than to implement these cause i still need to learn alot about the matrix operations on matlab i think..thats why i can not make any code by my own with these ideas... sigh ..frustrated – tina066 May 13 '15 at 01:28
  • @tina066 to access all elements of a specific diagonal I took advantage of Matlab's *linear indexing*. You can read more about it [here](http://www.mathworks.com/help/matlab/math/matrix-indexing.html). – Shai May 13 '15 at 08:05
  • @tina066 using linear indexing to access diagonal is explained very clearly at the second example of [this answer](http://stackoverflow.com/a/3963587/1714410). – Shai May 13 '15 at 08:07
  • thnx alot @shai really appreciate – tina066 May 14 '15 at 08:29
0

I often find that Matlab gives itself to a coding approach of "write what it says in the equation". That also leads to code that is easy to read...

A = [2 3; 4 1];
B = [3; 2];
Q = 4;

%// don't need to...
s = [];

%// ...but better to pre-allocate s for performance
s = zeros((Q+1)*2, Q);

X = B;
for m = 2:Q+1
    for n = m:Q+1
        s(n*2+(-1:0), n-m+1) = X;
    end
    X = A * X;
end
Shai
  • 111,146
  • 38
  • 238
  • 371
  • Of course, you can set Q to 60. I've chosen Q=4 so the result is readable. – Rattus Ex Machina May 12 '15 at 10:06
  • You are performing a lot of redundant computations, why not taking advantage of the fact that the diagonals are all the same? – Shai May 12 '15 at 10:18
  • That wasn't the question. And ease of reading is usually more helpful, pedagogically, right? Why -1 for a correct answer :)? (EDIT) Not to mention, btw, that my code is faster than yours. All that cell array manipulation, that's expensive stuff. Just sayin' :P – Rattus Ex Machina May 12 '15 at 10:22
  • Your answer is correct, therefore I do not vote to delete it. However, *IMHO*, it is not good even from the pedagogical point of view since it does not pre-allocate `s`, it involves a *nested* loop and lots of redundant computations. if you want to "write what's in the equation" you should follow the diagonals. – Shai May 12 '15 at 10:28
  • 1
    [**pre-allocate** `s`!](http://stackoverflow.com/questions/21023171/variable-appears-to-change-size-on-every-loop-iteration-what) – Shai May 12 '15 at 10:29
  • My first draft did pre-allocate s (I think I even posted it, did I...?). I actually removed it deliberately to show that you don't need it. This is a tiny operation, and it's important to teach clarity as well as performance, I figured. But yeah, pre-allocate s also makes sense, I completely agree. – Rattus Ex Machina May 12 '15 at 10:32
  • clarity is important, but the OP is a beginner and IMHO it is crucial to lend him good practices not only in clarity but also in performance consideration. – Shai May 12 '15 at 10:38