Path: news.mathworks.com!not-for-mail
From: "T " <tDO.Tdamsma@studentPO.INTtudelftPERI.ODnl>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Vectorize silly matrix
Date: Mon, 20 Apr 2009 12:04:01 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 35
Message-ID: <gshobh$a0q$1@fred.mathworks.com>
References: <2b9d378f-1af9-40c1-81dc-1ad16f709c85@c12g2000yqc.googlegroups.com> <gsgo49$ejh$1@fred.mathworks.com> <gshcki$4h5$1@fred.mathworks.com> <gshhah$jgp$1@fred.mathworks.com> <gshill$crv$1@fred.mathworks.com> <gshk2l$hib$1@fred.mathworks.com>
Reply-To: "T " <tDO.Tdamsma@studentPO.INTtudelftPERI.ODnl>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1240229041 10266 172.30.248.37 (20 Apr 2009 12:04:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 20 Apr 2009 12:04:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1484771
Xref: news.mathworks.com comp.soft-sys.matlab:534035

I came up with some other options, and I've plotted the performance as function of r 
d c for square matrices (r=c), see: http://www.damsma.net/performance.png

the fastest method really depends on the matrix size. Interesting that method 7, with a loop, is pretty fast. Also notice the strange behaviour at i=8 (r,c=512). Anyone have more suggestions for other methods?

1
for k = 1:r
    for m = 1:c
        A(k,m) = k+m-1;
    end
end

2
[x,y]=meshgrid(1:c,1:r);
A = x+y-1;

3
A = hankel(1:r,r:r+c-1);

4
A = cumsum([[1:r].' ones(r,c-1)],2);

5
A = bsxfun(@plus,[1:r].',1:c)-1;

6
A = repmat(1:c,r,1)+repmat((1:r)',1,c)-1;

7
A = ones(r,c);
for i=1:r
    A(i,:)=i:(c+i-1);
end

I tested for i=1:12, c,r = i^3, all using timeit, using matlab 2007b on a 2.41 ghz athlon 64 on WinXP with 1gb ram.