How to write this matrix in matlab,

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 tried this code below but gave an 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);

it seems that matrix concatenation is not being performed. I am new, so I have serious problems, please help.

+4
source share
2 answers

Use array cells and answer to 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 dealand cell2mat.


Important note on the difference between matrix operations and elementary operations

In your question (and in your previous one ) you confuse between the power of the matrix: A^2and the elementary operation A.^2:

  • Matrix power A^2 = [16 9;12 13]is the matrix productA*A
  • element-wise power A.^2 : A.^2 = [4 9; 16 1]

A*b, , , A.*b, -. , A b .

+1

, Matlab ", ". ...

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
0

All Articles