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:51:04 +0000 (UTC)
Organization: Xoran Technologies
Lines: 30
Message-ID: <h6kcvo$mkl$1@fred.mathworks.com>
References: <h0uj19$i02$1@fred.mathworks.com> <h6kcbr$c3k$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 1250801464 23189 172.30.248.38 (20 Aug 2009 20:51:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 20 Aug 2009 20:51:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:564899


"Matt " <xys@whatever.com> wrote in message <h6kcbr$c3k$1@fred.mathworks.com>...
> Here's a solution that meets the stated requirements, but I don't know if it saves anything. It uses kron, which is generally a slow operation

Actually, it does seem to save something. I did the following timing test on a larger data set. Note that my proposed solution constructs the final matrix B in sparse form, which saves some memory allocation ops. Conversely, blkdiag only returns its output in full form. However even when converting to full in method 1, I still find it to be considerably faster.


A=rand(1000,2);

%method 1
tic
[m,n]=size(A);
map=logical(kron(speye(m),ones(1,n)));
[M,N]=size(map);

B=spalloc(M,N,numel(A));
At=A.';
B(map)=At(:);

%B=full(B);

toc
%Elapsed time is 0.004230 seconds.

%Using blkdiag
tic
 C=num2cell(A,2);
 BB=blkdiag(C{:});

toc
%Elapsed time is 0.050917 seconds.