Creating a diagonal matrix which contains already defined matrix on diagonals??

I am trying to create a matrix "M" which contains the matrix "a" at its diagonals. The value of matrix "a" is changing over the variable "n". So "a" matrix is different for each "n". I want a matrix (suppose that it is of order 3by3, and in relation to n we say a=a(n)) so the output must be of form M=[a(1) 0 0;0 a(2) 0;0 0 a(3)],where each a(n)is a 3by3 matrix.
I made the following code, the problem is that when I try to replace entries of matrix "M" with a(n), Instead of inserting the whole matrix "a" at diagonal it just inserts the first value of "a" and repeat it. I am attaching a pdf file so that I can show what I actually want.
I will be grateful for your help.
if true
% code
clc
clear
Nt=3;Nm=3;b=1;
%Define E(j,k)
for k=0:Nt-1
for j=0:Nt-1
if j~=k
e(j+1,k+1)= 0;
elseif j==k~=0
e(j+1,k+1)= pi/2;
else e(j+1,k+1) = pi;
end
%Define D(j,k)
if mod(k-j,2)==0
d(j+1,k+1)= pi/2*k*(k^2-j^2);
else d(j+1,k+1)=0;
end
%Define A(n) matrix
for n=-Nm:Nm
a(j+1,k+1)=-(n*b)^2*e(j+1,k+1)+d(j+1,k+1);
M((n+Nm)*Nt+1:(n+Nm+1)*Nt,(n+Nm)*Nt+1:(n+Nm+1)*Nt)=a(j+1,k+1);
end
end
end
end

4 件のコメント

Star Strider
Star Strider 2014 年 6 月 12 日
Are you creating a banded matrix?
Cedric
Cedric 2014 年 6 月 12 日
It seems more block-diagonal actually .. (?)
dpb
dpb 2014 年 6 月 12 日
...when I try to replace entries of matrix "M" with a(n), Instead of inserting the whole matrix "a" at diagonal...
Make M a cell array, populate, then use cell2mat on the result, maybe? Of course, each cell will need to be the 3x3 or whatever size of a(n) is so the last operation works...
Syed Tayyab Hussain
Syed Tayyab Hussain 2014 年 6 月 12 日
I want to create a diagonal matrix "M" where matrix "a" are the diagonal entries. first diagonal entry will be a matrix "a" for n=1, second diagonal entry will be a matrix "a" for n=2 and so on..

サインインしてコメントする。

回答 (1 件)

Cedric
Cedric 2014 年 6 月 12 日
編集済み: Cedric 2014 年 6 月 12 日
Here is an example which builds a block diagonal matrix A based on 3x3, trivially time-dependent matrices
t = 0 : 3 ; % Define vector of times.
nt = length( t ) ;
a_t = cell( nt, 1 ) ; % Prealloc cell array for 3x3 matrices.
for tId = 1 : nt
a_t{tId} = t(tId) + ones( 3 ) ; % Define trivial time-dep. 3x3 matrix.
end
A = blkdiag( a_t{:} ) ; % Build block-diagonal, large matrix.
The last line uses BLKDIAG to create the block-diagonal A matrix, and passes the cell array a_t expended as a comma-separated list. It is equivalent to
A = blkdiag( a_t{1}, a_t{2}, a_t{3}, a_t{4} ) ;
without having to list explicitly all elements of the cell array, which is advantageous.
Running this code, you get e.g.
>> a_t{2}
ans =
2 2 2
2 2 2
2 2 2
and
>> A
A =
1 1 1 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 2 2 2 0 0 0 0 0 0
0 0 0 2 2 2 0 0 0 0 0 0
0 0 0 2 2 2 0 0 0 0 0 0
0 0 0 0 0 0 3 3 3 0 0 0
0 0 0 0 0 0 3 3 3 0 0 0
0 0 0 0 0 0 3 3 3 0 0 0
0 0 0 0 0 0 0 0 0 4 4 4
0 0 0 0 0 0 0 0 0 4 4 4
0 0 0 0 0 0 0 0 0 4 4 4

1 件のコメント

Cedric
Cedric 2014 年 6 月 12 日
編集済み: Cedric 2014 年 6 月 12 日
PS: I say time-dependent to make an example which depends on a parameter, but it doesn't need to be time. In your case, it will be n=[-3:-1,1:3] I guess. The important point is that it shows how to build these 3x3 matrices and store them in a cell array, and at the end only build the block-diagonal matrix in one shot.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOperating on Diagonal Matrices についてさらに検索

質問済み:

2014 年 6 月 12 日

編集済み:

2014 年 6 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by