Path: news.mathworks.com!not-for-mail
From: "Bruno Luong" <b.luong@fogale.findmycountry>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Preallocation in MATLAB R2009a
Date: Wed, 5 Aug 2009 18:47:03 +0000 (UTC)
Organization: FOGALE nanotech
Lines: 18
Message-ID: <h5ck37$7iv$1@fred.mathworks.com>
References: <h5c475$639$1@fred.mathworks.com> <h5c9nq$fsm$1@fred.mathworks.com> <h5cgs2$52$1@fred.mathworks.com> <h5chq4$3nt$1@fred.mathworks.com> <h5cikr$jh$1@fred.mathworks.com>
Reply-To: "Bruno Luong" <b.luong@fogale.findmycountry>
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 1249498023 7775 172.30.248.37 (5 Aug 2009 18:47:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 5 Aug 2009 18:47:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 390839
Xref: news.mathworks.com comp.soft-sys.matlab:561101


"us " <us@neurol.unizh.ch> wrote in message <h5cikr$jh$1@fred.mathworks.com>...

> tic;
>      r=zeros(n);
> toc

It does take much effort for Mathworks to treat *any* calling in a better way. Frankly, who think about specify class all the time when ZEROS is invoked? Certainly not me, and I must have thousands of unspecified-class zeros calls in my Matlab folder. Something like this would be a simple work around. 

function z = zeros(varargin)
if ~ischar(varargin{end})
   z = builtin('zeros', varargin{:}, 'double');
else
   z = builtin('zeros', varargin{:});
end

I suspect the slow speed is not due to overhead, but the built-in implementation does not use something like C MEMSET to fill the memory. They must add improve this function for the next release. 

Bruno