Column of matrices for arbitrary N.

2 ビュー (過去 30 日間)
Mike Leijten
Mike Leijten 2020 年 2 月 6 日
回答済み: Hank 2020 年 2 月 6 日
Hi,
I need to construct the vector of A matrix powers (called Phi) and the matrix of B AB (called Gamma) etc.
I'm not good at programming and I have been struggling with it for a while now.
Capture.PNG
The script i have made and the output is shown below:
A matrix of zeros is created and then correctly filled with the corresponding values from the variable "test". But I actually want to make the column [A ; A^2 ; ..... ; A^N] for an arbitrary N.
But as soon as I program A^N for N = 1:3 only the last output is shown.
Any help is greatly appreciated.

採用された回答

Hank
Hank 2020 年 2 月 6 日
Hi Mike.
The way you're building phi is creating a matrix, not a vector, since zeros(m,n) creates an mxn matrix.
I think this is what you're trying to do
function [Gamma,phi] = GAMMA(A,B,N)
% A and B are a scalar
phi = A.^(1:N)'; % raises A to powers 1,2,3,...N, creates a column vector
Gamma = diag( B * ones(N,1) ); % create gamma, a diagonal matrix of B
for i=1:N
for j=1:i-1
Gamma(i,j) = B * phi(i-j); % element i-j is A^(i-j)
end
end
end
I tested this with [gam,phi] = GAMMA(2,1,5);
Seems to work alright.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by