Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: construct diagonal block matrix
Date: Thu, 20 Aug 2009 20:08:04 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 29
Message-ID: <h6kaf4$8fm$1@fred.mathworks.com>
References: <h0uj19$i02$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1250798884 8694 172.30.248.38 (20 Aug 2009 20:08:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 20 Aug 2009 20:08:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1941167
Xref: news.mathworks.com comp.soft-sys.matlab:564878


Hi Tim,

I have a solution.  Let

M = [1 2; 3 4; 5 6];

Then, the matrix you are looking for can be computed with the following two lines:
A = [diag(M(:,1)) diag(M(:,2))];
B = A(:,reshape(reshape(1:6,3,2)',1,6))

Hope this helps,
Danny

"Tim Yang" <dlISCool@gmail.com> wrote in message <h0uj19$i02$1@fred.mathworks.com>...
> I want to build a diagonal matrix such as
> 
> 1 2 0 0 0 0
> 0 0 3 4 0 0 
> 0 0 0 0 5 6 
> 
> with a given (arbitrary) matrix,   
> 1 2
> 3 4
> 5 6
> 
> without using loops and cell arrays (conversion takes time)  blkdiag works with only parameters (a,b,c,d...) a,b,c,d... are row vectors.
> suggestions?
> 
> Tim