Thread Subject: Biulding a diagonal matrix from submatrices

Subject: Biulding a diagonal matrix from submatrices

From: Jal

Date: 18 Mar, 2010 19:17:04

Message: 1 of 6

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.

Subject: Biulding a diagonal matrix from submatrices

From: Jos (10584)

Date: 18 Mar, 2010 19:29:21

Message: 2 of 6

"Jal" <jal@mathworks.com> wrote in message <hntu7g$plp$1@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

Subject: Biulding a diagonal matrix from submatrices

From: Matt J

Date: 18 Mar, 2010 19:33:04

Message: 3 of 6

"Jal" <jal@mathworks.com> wrote in message <hntu7g$plp$1@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

Subject: Biulding a diagonal matrix from submatrices

From: Bruno Luong

Date: 18 Mar, 2010 20:09:04

Message: 4 of 6

A = blkdiag(p1,p2,p3)

Bruno

Subject: Biulding a diagonal matrix from submatrices

From: Jal

Date: 19 Mar, 2010 05:04:03

Message: 5 of 6

"Jal" <jal@mathworks.com> wrote in message <hntu7g$plp$1@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.

Subject: Biulding a diagonal matrix from submatrices

From: Bruno Luong

Date: 19 Mar, 2010 07:06:04

Message: 6 of 6

For such matrix, you might want to store in sparse format.

for i=1:10000
  p{i}=sparse(i*eye(3));
end

A = blkdiag(p{:});

Bruno

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
kronprod Matt J 18 Mar, 2010 15:34:17
rssFeed for this Thread

Contact us at files@mathworks.com