From: Jal on
Hi,
I am trying to build up a diagonal matrix (A) of 3x(nxn) dimension,where n is the number of paths. Each path (p) is represented by a 3x3 matrix.
Example: Let's assume we have three paths (n=3).
p1=[1 1 1;1 1 1;1 1 1];
p2=[2 2 2;2 2 2;2 2 2];
p3=[3 3 3;3 3 3;3 3 3];

The diagonal matrix (A) would look like:
A =

1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
1 1 1 0 0 0 0 0 0
0 0 0 2 2 2 0 0 0
0 0 0 2 2 2 0 0 0
0 0 0 2 2 2 0 0 0
0 0 0 0 0 0 3 3 3
0 0 0 0 0 0 3 3 3
0 0 0 0 0 0 3 3 3

Note the number of paths could be any number.
I really appreciate your help.
From: Jos (10584) on
"Jal" <jal(a)mathworks.com> wrote in message <hntu7g$plp$1(a)fred.mathworks.com>...
> Hi,
> I am trying to build up a diagonal matrix (A) of 3x(nxn) dimension,where n is the number of paths. Each path (p) is represented by a 3x3 matrix.
> Example: Let's assume we have three paths (n=3).
> p1=[1 1 1;1 1 1;1 1 1];
> p2=[2 2 2;2 2 2;2 2 2];
> p3=[3 3 3;3 3 3;3 3 3];
>
> The diagonal matrix (A) would look like:
> A =
>
> 1 1 1 0 0 0 0 0 0
> 1 1 1 0 0 0 0 0 0
> 1 1 1 0 0 0 0 0 0
> 0 0 0 2 2 2 0 0 0
> 0 0 0 2 2 2 0 0 0
> 0 0 0 2 2 2 0 0 0
> 0 0 0 0 0 0 3 3 3
> 0 0 0 0 0 0 3 3 3
> 0 0 0 0 0 0 3 3 3
>
> Note the number of paths could be any number.
> I really appreciate your help.

Here is one way:

% your data, stored as cell arrays (see the FAQ)
p{1} = ones(3) ;
p{2} = p{1}+1 ;
p{3} = p{1}+2 ;

%engine
c = repmat({zeros(numel(p))},size(p{1})) ;
[c{eye(numel(p))==1}] = deal(p{:}) ;
D = cell2mat(c)

hth
Jos
From: Matt J on
"Jal" <jal(a)mathworks.com> wrote in message <hntu7g$plp$1(a)fred.mathworks.com>...
> Hi,
> I am trying to build up a diagonal matrix (A) of 3x(nxn) dimension,where n is the number of paths. Each path (p) is represented by a 3x3 matrix.
> Example: Let's assume we have three paths (n=3).
> p1=[1 1 1;1 1 1;1 1 1];
> p2=[2 2 2;2 2 2;2 2 2];
> p3=[3 3 3;3 3 3;3 3 3];

Do the p matrices always consist of identical values, and are they always consecutive integers, as in your example? If so, a lazy way to construct this is by taking Kronecker products

A=kron(sparse(diag(1:n)),ones(3));

However, it is rare to find an application where you would want to construct such a Kronecker product matrix explicitly. It's a very large matrix with a lot of redundant information. I created this FEX tool as a more efficient alternative,

http://www.mathworks.com/matlabcentral/fileexchange/25969-efficient-object-oriented-kronecker-product-manipulation
From: Bruno Luong on
A = blkdiag(p1,p2,p3)

Bruno
From: Jal on
"Jal" <jal(a)mathworks.com> wrote in message <hntu7g$plp$1(a)fred.mathworks.com>...
> Hi,
> I am trying to build up a diagonal matrix (A) of 3x(nxn) dimension,where n is the number of paths. Each path (p) is represented by a 3x3 matrix.
> Example: Let's assume we have three paths (n=3).
> p1=[1 1 1;1 1 1;1 1 1];
> p2=[2 2 2;2 2 2;2 2 2];
> p3=[3 3 3;3 3 3;3 3 3];
>
> The diagonal matrix (A) would look like:
> A =
>
> 1 1 1 0 0 0 0 0 0
> 1 1 1 0 0 0 0 0 0
> 1 1 1 0 0 0 0 0 0
> 0 0 0 2 2 2 0 0 0
> 0 0 0 2 2 2 0 0 0
> 0 0 0 2 2 2 0 0 0
> 0 0 0 0 0 0 3 3 3
> 0 0 0 0 0 0 3 3 3
> 0 0 0 0 0 0 3 3 3
>
> Note the number of paths could be any number.
> I really appreciate your help.

Hi guys,
Thanks a lot for your reply. i will try blkdiag function. I do not know if there is any a better way to do it ,because this function consumes time. As for the matrices entries,they could be complex numbers.