Path: news.mathworks.com!newsfeed-00.mathworks.com!newscon02.news.prodigy.net!prodigy.net!news.glorb.com!postnews.google.com!s50g2000hsb.googlegroups.com!not-for-mail
From: Rex <rex.eastbourne@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to be a good Matlab programmer
Date: Sat, 19 Apr 2008 17:23:06 -0700 (PDT)
Organization: http://groups.google.com
Lines: 41
Message-ID: <b375ae00-dba3-42a5-a138-31be2142591c@s50g2000hsb.googlegroups.com>
References: <d6f9728b-07d7-4ebc-8264-e2485cde6259@c65g2000hsa.googlegroups.com> 
NNTP-Posting-Host: 140.247.249.90
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1208650987 28578 127.0.0.1 (20 Apr 2008 00:23:07 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Sun, 20 Apr 2008 00:23:07 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: s50g2000hsb.googlegroups.com; posting-host=140.247.249.90; 
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.14) 
Xref: news.mathworks.com comp.soft-sys.matlab:464087



On Apr 19, 7:23 pm, dpb <n...@non.net> wrote:
> Rex wrote:
> > Hello,
>
> > I was wondering if any of you could suggest any resources for learning
> > idioms/tricks of effective Matlab programming. I want to make sure I'm
> > writing code in proper Matlab style. As a very simple example,
> > although I am used to writing "for" loops in Python/Ruby/C, I have
> > learned that in Matlab, vectorizing my code is faster and more
> > concise. I am sure there are other useful tips for people coming from
> > different languages. Any resources on this?
>
> I assume you've read the section in the online documentation on
> "Optimizing Matlab Code" in the m-file programming section?
>
> --

Yes, I have; that is the type of resource I am looking for. However, I
want to be clear that I'm not just looking for tips on optimizing the
performance of my code;  I also want to learn to write more elegant
and concise code. For example, here's an example of how I used to
write for loops:

dims = [10 18 21 25 40 65];
[a,b] = size(dims);
for i = 1:b,
    dlmwrite(sprintf('matrix%d', dims(b)), ones(dims(b)));
end

A friend recently pointed out that I can refactor it as follows:

dims = [10 18 21 25 40 65];
for i = dims,
    dlmwrite(sprintf('matrix%d', i), ones(i));
end

This might be common sense, but I was so used to the idioms of other
programming languages that I didn't think of refactoring my code this
way.

Rex