<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906</link>
    <title>MATLAB Central Newsreader - How to be a good Matlab programmer</title>
    <description>Feed for thread: How to be a good Matlab programmer</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sat, 19 Apr 2008 23:02:01 -0400</pubDate>
      <title>How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427739</link>
      <author>Rex</author>
      <description>Hello,&lt;br&gt;
&lt;br&gt;
I was wondering if any of you could suggest any resources for learning&lt;br&gt;
idioms/tricks of effective Matlab programming. I want to make sure I'm&lt;br&gt;
writing code in proper Matlab style. As a very simple example,&lt;br&gt;
although I am used to writing &quot;for&quot; loops in Python/Ruby/C, I have&lt;br&gt;
learned that in Matlab, vectorizing my code is faster and more&lt;br&gt;
concise. I am sure there are other useful tips for people coming from&lt;br&gt;
different languages. Any resources on this?&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Rex</description>
    </item>
    <item>
      <pubDate>Sat, 19 Apr 2008 23:23:44 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427741</link>
      <author>dpb</author>
      <description>Rex wrote:&lt;br&gt;
&amp;gt; Hello,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I was wondering if any of you could suggest any resources for learning&lt;br&gt;
&amp;gt; idioms/tricks of effective Matlab programming. I want to make sure I'm&lt;br&gt;
&amp;gt; writing code in proper Matlab style. As a very simple example,&lt;br&gt;
&amp;gt; although I am used to writing &quot;for&quot; loops in Python/Ruby/C, I have&lt;br&gt;
&amp;gt; learned that in Matlab, vectorizing my code is faster and more&lt;br&gt;
&amp;gt; concise. I am sure there are other useful tips for people coming from&lt;br&gt;
&amp;gt; different languages. Any resources on this?&lt;br&gt;
&lt;br&gt;
I assume you've read the section in the online documentation on &lt;br&gt;
&quot;Optimizing Matlab Code&quot; in the m-file programming section?&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 00:23:06 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427745</link>
      <author>Rex</author>
      <description>On Apr 19, 7:23 pm, dpb &amp;lt;n...@non.net&amp;gt; wrote:&lt;br&gt;
&amp;gt; Rex wrote:&lt;br&gt;
&amp;gt; &amp;gt; Hello,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I was wondering if any of you could suggest any resources for learning&lt;br&gt;
&amp;gt; &amp;gt; idioms/tricks of effective Matlab programming. I want to make sure I'm&lt;br&gt;
&amp;gt; &amp;gt; writing code in proper Matlab style. As a very simple example,&lt;br&gt;
&amp;gt; &amp;gt; although I am used to writing &quot;for&quot; loops in Python/Ruby/C, I have&lt;br&gt;
&amp;gt; &amp;gt; learned that in Matlab, vectorizing my code is faster and more&lt;br&gt;
&amp;gt; &amp;gt; concise. I am sure there are other useful tips for people coming from&lt;br&gt;
&amp;gt; &amp;gt; different languages. Any resources on this?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I assume you've read the section in the online documentation on&lt;br&gt;
&amp;gt; &quot;Optimizing Matlab Code&quot; in the m-file programming section?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; --&lt;br&gt;
&lt;br&gt;
Yes, I have; that is the type of resource I am looking for. However, I&lt;br&gt;
want to be clear that I'm not just looking for tips on optimizing the&lt;br&gt;
performance of my code;  I also want to learn to write more elegant&lt;br&gt;
and concise code. For example, here's an example of how I used to&lt;br&gt;
write for loops:&lt;br&gt;
&lt;br&gt;
dims = [10 18 21 25 40 65];&lt;br&gt;
[a,b] = size(dims);&lt;br&gt;
for i = 1:b,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dlmwrite(sprintf('matrix%d', dims(b)), ones(dims(b)));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
A friend recently pointed out that I can refactor it as follows:&lt;br&gt;
&lt;br&gt;
dims = [10 18 21 25 40 65];&lt;br&gt;
for i = dims,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dlmwrite(sprintf('matrix%d', i), ones(i));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
This might be common sense, but I was so used to the idioms of other&lt;br&gt;
programming languages that I didn't think of refactoring my code this&lt;br&gt;
way.&lt;br&gt;
&lt;br&gt;
Rex</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 01:38:35 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427748</link>
      <author>Dave Bell</author>
      <description>Rex wrote:&lt;br&gt;
&amp;gt; On Apr 19, 7:23 pm, dpb &amp;lt;n...@non.net&amp;gt; wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; I assume you've read the section in the online documentation on&lt;br&gt;
&amp;gt;&amp;gt; &quot;Optimizing Matlab Code&quot; in the m-file programming section?&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; --&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Yes, I have; that is the type of resource I am looking for. However, I&lt;br&gt;
&amp;gt; want to be clear that I'm not just looking for tips on optimizing the&lt;br&gt;
&amp;gt; performance of my code;  I also want to learn to write more elegant&lt;br&gt;
&amp;gt; and concise code.&lt;br&gt;
&amp;gt; Rex&lt;br&gt;
&lt;br&gt;
That was a good example of what I encountered as a new Matlab user, as &lt;br&gt;
well. It's an entirely different programming paradigm, and it takes a &lt;br&gt;
while to get used to. (I still program Matlab like C!)&lt;br&gt;
&lt;br&gt;
Another issue I ran into was simply searching for help. When I was &lt;br&gt;
trying to speed up a project I was working on, someone suggested using &lt;br&gt;
benchmark before and after changing some code. Not wanting to seem &lt;br&gt;
dumber than I was, I didn't ask how to benchmark my code, I just figure &lt;br&gt;
I'd look it up. Try it - there is no such command. One of the two hits I &lt;br&gt;
got from 'Help benchmark' was an article on training a neural net, the &lt;br&gt;
other was about a Simulink model of a Physics 1 experiment with a cart &lt;br&gt;
and mass. I won't admit to how long it took to find 'bench'!&lt;br&gt;
&lt;br&gt;
Dave</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 03:41:53 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427752</link>
      <author>NZTideMan</author>
      <description>On Apr 20, 1:38=A0pm, Dave Bell &amp;lt;db...@TheSPAMFREEBells.net&amp;gt; wrote:&lt;br&gt;
&amp;gt; Rex wrote:&lt;br&gt;
&amp;gt; &amp;gt; On Apr 19, 7:23 pm, dpb &amp;lt;n...@non.net&amp;gt; wrote:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; I assume you've read the section in the online documentation on&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; &quot;Optimizing Matlab Code&quot; in the m-file programming section?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; --&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Yes, I have; that is the type of resource I am looking for. However, I&lt;br&gt;
&amp;gt; &amp;gt; want to be clear that I'm not just looking for tips on optimizing the&lt;br&gt;
&amp;gt; &amp;gt; performance of my code; =A0I also want to learn to write more elegant&lt;br&gt;
&amp;gt; &amp;gt; and concise code.&lt;br&gt;
&amp;gt; &amp;gt; Rex&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; That was a good example of what I encountered as a new Matlab user, as&lt;br&gt;
&amp;gt; well. It's an entirely different programming paradigm, and it takes a&lt;br&gt;
&amp;gt; while to get used to. (I still program Matlab like C!)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Another issue I ran into was simply searching for help. When I was&lt;br&gt;
&amp;gt; trying to speed up a project I was working on, someone suggested using&lt;br&gt;
&amp;gt; benchmark before and after changing some code. Not wanting to seem&lt;br&gt;
&amp;gt; dumber than I was, I didn't ask how to benchmark my code, I just figure&lt;br&gt;
&amp;gt; I'd look it up. Try it - there is no such command. One of the two hits I&lt;br&gt;
&amp;gt; got from 'Help benchmark' was an article on training a neural net, the&lt;br&gt;
&amp;gt; other was about a Simulink model of a Physics 1 experiment with a cart&lt;br&gt;
&amp;gt; and mass. I won't admit to how long it took to find 'bench'!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Dave&lt;br&gt;
&lt;br&gt;
There are some lessons we learn from bitter experience.  Like (using&lt;br&gt;
your example):&lt;br&gt;
dims =3D [10 18 21 25 40 65];&lt;br&gt;
for i =3D dims,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dlmwrite(sprintf('matrix%d', i), ones(i));&lt;br&gt;
end&lt;br&gt;
-&lt;br&gt;
-&lt;br&gt;
y=3Damp.*exp(i*phase);&lt;br&gt;
&lt;br&gt;
But now, i=3D65, not sqrt(-1).&lt;br&gt;
Lesson: NEVER use i or j as an index&lt;br&gt;
&lt;br&gt;
Even if this were in a textbook somewhere (and it probably is), I&lt;br&gt;
think you must make the mistake in order to learn your folly.&lt;br&gt;
&lt;br&gt;
But, IMHO, the best way to learn style is to read CSSM, especially&lt;br&gt;
from posters such as &quot;us&quot;.  If you read his posts and follow his code&lt;br&gt;
(even if it is irrelevant to you), you will learn lots about style&lt;br&gt;
(and new tricks as well).</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 04:34:06 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427756</link>
      <author>Timo Nieminen</author>
      <description>On Sat, 19 Apr 2008, Rex wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; I also want to learn to write more elegant&lt;br&gt;
&amp;gt; and concise code. For example, here's an example of how I used to&lt;br&gt;
&amp;gt; write for loops:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; dims = [10 18 21 25 40 65];&lt;br&gt;
&amp;gt; [a,b] = size(dims);&lt;br&gt;
&amp;gt; for i = 1:b,&lt;br&gt;
&amp;gt;     dlmwrite(sprintf('matrix%d', dims(b)), ones(dims(b)));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A friend recently pointed out that I can refactor it as follows:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; dims = [10 18 21 25 40 65];&lt;br&gt;
&amp;gt; for i = dims,&lt;br&gt;
&amp;gt;     dlmwrite(sprintf('matrix%d', i), ones(i));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
&quot;Concise&quot; is not always good. &quot;Readable&quot; is always good. If conciseness &lt;br&gt;
reduces readability, then it's bad - longer code that's easier to read, &lt;br&gt;
understand, debug, and maintain is better. Often enough, even slower code &lt;br&gt;
can be better if it's easier to read, debug, and maintain, for example, &lt;br&gt;
if said code occupies less than 1% of the runtime.&lt;br&gt;
&lt;br&gt;
Consider the joys of ultra-concise C code ...&lt;br&gt;
&lt;br&gt;
While your example above is short enough to not suffer, if &quot;dims = ...&quot; &lt;br&gt;
was a long way from the loop, and the loop was longer, the reader might &lt;br&gt;
well assume that &quot;i&quot; is an integer loop counter when reading the code. &lt;br&gt;
Apart from using i or j as counters being bad practice in matlab &lt;br&gt;
(sqrt(-1) and all that), &quot;for dim = dims&quot; is better for readability.&lt;br&gt;
&lt;br&gt;
Possible tips for matlab programmers coming from other languages:&lt;br&gt;
&lt;br&gt;
(a) Matrix operations are simple and readable. Much more so than loops.&lt;br&gt;
&lt;br&gt;
(b) You don't need to declare variables. Thus, beware of mistyped (or &lt;br&gt;
easily mistypeable) variable names - you won't have a compiler telling you &lt;br&gt;
it doesn't know what the new variable is.&lt;br&gt;
&lt;br&gt;
(c) When performing calculations using a formula from some source, stick &lt;br&gt;
to the original notation as far as reasonably possible. For example, have &lt;br&gt;
a variable &quot;tau&quot; rather than &quot;OpticalDepth&quot;. Some might disagree with &lt;br&gt;
this, given that &quot;OpticalDepth&quot; is more descriptive than &quot;tau&quot;, but &lt;br&gt;
assuming you and other readers of the code are familiar with the field, &lt;br&gt;
having matlab code that looks like the formulae in the textbooks is easier &lt;br&gt;
to read IMHO.&lt;br&gt;
&lt;br&gt;
(d) As above, don't use i or j as counters.&lt;br&gt;
&lt;br&gt;
(e) Functions can return more than one value, e.g., [a,b,c] = foo(d,e).&lt;br&gt;
&lt;br&gt;
(f) You can check how many output values are asked for when a function is &lt;br&gt;
called (and, of course, how many input values are passed to the function).&lt;br&gt;
&lt;br&gt;
(g) When you write a function, include the text that will be displayed &lt;br&gt;
when the user types &quot;help foo&quot;. Make this text useful.&lt;br&gt;
&lt;br&gt;
(h) Matlab is nice for debugging. If running scripts, you have access to &lt;br&gt;
all of the variables in memory if it crashes. What about functions? One &lt;br&gt;
approach is to start with it as a script, and then turn it into a &lt;br&gt;
function. Or, use &quot;keyboard&quot; to interrupt execution and give you access to &lt;br&gt;
the function's memory workspace. Also use &quot;keyboard&quot; to interrupt a script &lt;br&gt;
at a particular point.&lt;br&gt;
&lt;br&gt;
(i) &quot;whos&quot; is a useful debugging tool.&lt;br&gt;
&lt;br&gt;
(j) The profiler (&quot;help profile&quot;) can be very useful to help you decide &lt;br&gt;
what parts of your code are worth trying to optmise.&lt;br&gt;
&lt;br&gt;
(k) It's easy to use external programs or OS commands (&quot;help system&quot;).&lt;br&gt;
&lt;br&gt;
(l) While Matlab has functions like &quot;strcat&quot;, it can be easier to read, &lt;br&gt;
and more concise, to write things like &quot;a = [ foo bar 'and more text' ];&quot;. &lt;br&gt;
This doesn't behave the same as strcat in general, but in the most common &lt;br&gt;
cases, it will.&lt;br&gt;
&lt;br&gt;
(m) While you can edit graphs etc using the GUI figure editor (e.g., &lt;br&gt;
changing line styles and colours, line thicknesses, font sizes, etc.), you &lt;br&gt;
can do this kind of thing via the command line. If you need to do it more &lt;br&gt;
than once, and especially when you need to do it the same way more than &lt;br&gt;
once, a few lines of code (in a function, or easily copy-and-pasted) can &lt;br&gt;
make this easy compared to remembering what you did last time (possibly &lt;br&gt;
months ago).&lt;br&gt;
&lt;br&gt;
(o) Graphs can also be good debugging tools.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Timo, who has written much bad Matlab code</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 09:18:04 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427775</link>
      <author>Tim Davis</author>
      <description>Timo Nieminen &amp;lt;timo@physics.uq.edu.au&amp;gt; wrote in message &lt;br&gt;
...&lt;br&gt;
&lt;br&gt;
&amp;gt; While your example above is short enough to not suffer, if&lt;br&gt;
&quot;dims = ...&quot; &lt;br&gt;
&amp;gt; was a long way from the loop, and the loop was longer, the&lt;br&gt;
reader might &lt;br&gt;
&amp;gt; well assume that &quot;i&quot; is an integer loop counter when&lt;br&gt;
reading the code. &lt;br&gt;
&amp;gt; Apart from using i or j as counters being bad practice in&lt;br&gt;
matlab &lt;br&gt;
&amp;gt; (sqrt(-1) and all that), &quot;for dim = dims&quot; is better for&lt;br&gt;
readability.&lt;br&gt;
&lt;br&gt;
I have to disagree about the use of i and j.  One should&lt;br&gt;
never rely on i or j as the imaginary unit value, since they&lt;br&gt;
can be masked by variables.  Use 1i instead, which can never&lt;br&gt;
be overwritten by a variable.  It is always a constant equal&lt;br&gt;
to sqrt(-1).  The variables i and j are not always equal to&lt;br&gt;
sqrt(-1) and should thus not be relied upon.&lt;br&gt;
&lt;br&gt;
The variables i and j have been used since time immemorial&lt;br&gt;
as matrix row and column indices.  Stick with standard&lt;br&gt;
mathematical notation and use them as such.&lt;br&gt;
&lt;br&gt;
I personally think that the choice made, a long while back,&lt;br&gt;
to use &quot;i&quot; as sqrt(-1) was a mistake.</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 14:12:29 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427798</link>
      <author>dpb</author>
      <description>Tim Davis wrote:&lt;br&gt;
...&lt;br&gt;
&amp;gt; The variables i and j have been used since time immemorial&lt;br&gt;
&amp;gt; as matrix row and column indices.  Stick with standard&lt;br&gt;
&amp;gt; mathematical notation and use them as such.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I personally think that the choice made, a long while back,&lt;br&gt;
&amp;gt; to use &quot;i&quot; as sqrt(-1) was a mistake.&lt;br&gt;
&lt;br&gt;
But 'i' (and 'j') have been used since time immemorial as as sqrt(-1) so &lt;br&gt;
TMW simply stuck w/ standard mathematical notation.  :)&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 14:25:00 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427800</link>
      <author>dpb</author>
      <description>Dave Bell wrote:&lt;br&gt;
&amp;gt; Rex wrote:&lt;br&gt;
...&lt;br&gt;
&amp;gt;&amp;gt; want to be clear that I'm not just looking for tips on optimizing the&lt;br&gt;
&amp;gt;&amp;gt; performance of my code;  I also want to learn to write more elegant&lt;br&gt;
&amp;gt;&amp;gt; and concise code.&lt;br&gt;
...&lt;br&gt;
&amp;gt; Another issue I ran into was simply searching for help. When I was &lt;br&gt;
&amp;gt; trying to speed up a project I was working on, someone suggested using &lt;br&gt;
&amp;gt; benchmark before and after changing some code. Not wanting to seem &lt;br&gt;
&amp;gt; dumber than I was, I didn't ask how to benchmark my code, I just figure &lt;br&gt;
&amp;gt; I'd look it up. Try it - there is no such command. One of the two hits I &lt;br&gt;
&amp;gt; got from 'Help benchmark' was an article on training a neural net, the &lt;br&gt;
&amp;gt; other was about a Simulink model of a Physics 1 experiment with a cart &lt;br&gt;
&amp;gt; and mass. I won't admit to how long it took to find 'bench'!&lt;br&gt;
&lt;br&gt;
doc lookfor&lt;br&gt;
&lt;br&gt;
and, of course, now one can also search the help files that wasn't &lt;br&gt;
particularly feasible in the early days&lt;br&gt;
&lt;br&gt;
I think as someone else noted simply reading code and following c.s-s.m &lt;br&gt;
and more importantly, practice are the keys.  Matlab is a rich language &lt;br&gt;
and consequently requires effort and time to gain proficiency.&lt;br&gt;
&lt;br&gt;
Maybe somebody will jump in during the next work week and can comment on &lt;br&gt;
their favorite texts that might be beneficial in the vein of the thread. &lt;br&gt;
&amp;nbsp;&amp;nbsp;W/ dialup I don't use the TMW web site nearly enough to be up on what &lt;br&gt;
is there but I'd be surprised if aren't archives of the tutorial and &lt;br&gt;
other resources there as well.  Steve Lord can probably stick in a few &lt;br&gt;
pointers there as well as others...&lt;br&gt;
&lt;br&gt;
--&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
--</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 19:50:03 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427834</link>
      <author>Duane Hanselman</author>
      <description>Rex &amp;lt;rex.eastbourne@gmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;d6f9728b-07d7-4ebc-8264-e2485cde6259@c65g2000hsa.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Hello,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I was wondering if any of you could suggest any resources&lt;br&gt;
for learning&lt;br&gt;
&amp;gt; idioms/tricks of effective Matlab programming. I want to&lt;br&gt;
make sure I'm&lt;br&gt;
&amp;gt; writing code in proper Matlab style. As a very simple example,&lt;br&gt;
&amp;gt; although I am used to writing &quot;for&quot; loops in&lt;br&gt;
Python/Ruby/C, I have&lt;br&gt;
&amp;gt; learned that in Matlab, vectorizing my code is faster and more&lt;br&gt;
&amp;gt; concise. I am sure there are other useful tips for people&lt;br&gt;
coming from&lt;br&gt;
&amp;gt; different languages. Any resources on this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Rex&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
How about a book, cowritten by a 25 year user of MATLAB?&lt;br&gt;
Mastering MATLAB 7. It covers MATLAB programming, especially&lt;br&gt;
the chapter entitled Examples, Examples, Examples.&lt;br&gt;
&lt;br&gt;
I used &quot;classic&quot; MATLAB in 1983 and purchased PC MATLAB in&lt;br&gt;
1985 from the company owner's California garage. (Isn't that&lt;br&gt;
where all California companies get their start?).&lt;br&gt;
&lt;br&gt;
Duane</description>
    </item>
    <item>
      <pubDate>Sun, 20 Apr 2008 22:09:03 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427849</link>
      <author>Timo Nieminen</author>
      <description>On Sun, 20 Apr 2008, Tim Davis wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; Timo Nieminen &amp;lt;timo@physics.uq.edu.au&amp;gt; wrote in message &lt;br&gt;
&amp;gt; ...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; While your example above is short enough to not suffer, if&lt;br&gt;
&amp;gt; &quot;dims = ...&quot; &lt;br&gt;
&amp;gt; &amp;gt; was a long way from the loop, and the loop was longer, the&lt;br&gt;
&amp;gt; reader might &lt;br&gt;
&amp;gt; &amp;gt; well assume that &quot;i&quot; is an integer loop counter when&lt;br&gt;
&amp;gt; reading the code. &lt;br&gt;
&amp;gt; &amp;gt; Apart from using i or j as counters being bad practice in&lt;br&gt;
&amp;gt; matlab &lt;br&gt;
&amp;gt; &amp;gt; (sqrt(-1) and all that), &quot;for dim = dims&quot; is better for&lt;br&gt;
&amp;gt; readability.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have to disagree about the use of i and j.  One should&lt;br&gt;
&amp;gt; never rely on i or j as the imaginary unit value, since they&lt;br&gt;
&amp;gt; can be masked by variables.  Use 1i instead, which can never&lt;br&gt;
&amp;gt; be overwritten by a variable.  It is always a constant equal&lt;br&gt;
&amp;gt; to sqrt(-1).  The variables i and j are not always equal to&lt;br&gt;
&amp;gt; sqrt(-1) and should thus not be relied upon.&lt;br&gt;
&lt;br&gt;
Excellent advice for robust programming. Especially for scripts.&lt;br&gt;
&lt;br&gt;
&amp;gt; The variables i and j have been used since time immemorial&lt;br&gt;
&amp;gt; as matrix row and column indices.  Stick with standard&lt;br&gt;
&amp;gt; mathematical notation and use them as such.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I personally think that the choice made, a long while back,&lt;br&gt;
&amp;gt; to use &quot;i&quot; as sqrt(-1) was a mistake.&lt;br&gt;
&lt;br&gt;
Perhaps, but now we must live with it.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Timo Nieminen - Home page: &lt;a href=&quot;http://www.physics.uq.edu.au/people/nieminen/&quot;&gt;http://www.physics.uq.edu.au/people/nieminen/&lt;/a&gt;&lt;br&gt;
E-prints: &lt;a href=&quot;http://eprint.uq.edu.au/view/person/Nieminen,_Timo_A..html&quot;&gt;http://eprint.uq.edu.au/view/person/Nieminen,_Timo_A..html&lt;/a&gt;&lt;br&gt;
Shrine to Spirits: &lt;a href=&quot;http://www.users.bigpond.com/timo_nieminen/spirits.html&quot;&gt;http://www.users.bigpond.com/timo_nieminen/spirits.html&lt;/a&gt;</description>
    </item>
    <item>
      <pubDate>Mon, 21 Apr 2008 00:32:02 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427866</link>
      <author>Tim Davis</author>
      <description>Timo Nieminen &amp;lt;timo@physics.uq.edu.au&amp;gt; wrote in message&lt;br&gt;
&amp;lt;Pine.LNX.4.50.0804210807080.8195-100000@localhost&amp;gt;...&lt;br&gt;
&amp;gt; On Sun, 20 Apr 2008, Tim Davis wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Timo Nieminen &amp;lt;timo@physics.uq.edu.au&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;gt; ...&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; While your example above is short enough to not suffer, if&lt;br&gt;
&amp;gt; &amp;gt; &quot;dims = ...&quot; &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; was a long way from the loop, and the loop was longer, the&lt;br&gt;
&amp;gt; &amp;gt; reader might &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; well assume that &quot;i&quot; is an integer loop counter when&lt;br&gt;
&amp;gt; &amp;gt; reading the code. &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; Apart from using i or j as counters being bad practice in&lt;br&gt;
&amp;gt; &amp;gt; matlab &lt;br&gt;
&amp;gt; &amp;gt; &amp;gt; (sqrt(-1) and all that), &quot;for dim = dims&quot; is better for&lt;br&gt;
&amp;gt; &amp;gt; readability.&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I have to disagree about the use of i and j.  One should&lt;br&gt;
&amp;gt; &amp;gt; never rely on i or j as the imaginary unit value, since they&lt;br&gt;
&amp;gt; &amp;gt; can be masked by variables.  Use 1i instead, which can never&lt;br&gt;
&amp;gt; &amp;gt; be overwritten by a variable.  It is always a constant equal&lt;br&gt;
&amp;gt; &amp;gt; to sqrt(-1).  The variables i and j are not always equal to&lt;br&gt;
&amp;gt; &amp;gt; sqrt(-1) and should thus not be relied upon.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Excellent advice for robust programming. Especially for&lt;br&gt;
scripts.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; The variables i and j have been used since time immemorial&lt;br&gt;
&amp;gt; &amp;gt; as matrix row and column indices.  Stick with standard&lt;br&gt;
&amp;gt; &amp;gt; mathematical notation and use them as such.&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I personally think that the choice made, a long while back,&lt;br&gt;
&amp;gt; &amp;gt; to use &quot;i&quot; as sqrt(-1) was a mistake.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Perhaps, but now we must live with it.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt; Timo Nieminen - Home page:&lt;br&gt;
&lt;a href=&quot;http://www.physics.uq.edu.au/people/nieminen/&quot;&gt;http://www.physics.uq.edu.au/people/nieminen/&lt;/a&gt;&lt;br&gt;
&amp;gt; E-prints:&lt;br&gt;
&lt;a href=&quot;http://eprint.uq.edu.au/view/person/Nieminen,_Timo_A..html&quot;&gt;http://eprint.uq.edu.au/view/person/Nieminen,_Timo_A..html&lt;/a&gt;&lt;br&gt;
&amp;gt; Shrine to Spirits:&lt;br&gt;
&lt;a href=&quot;http://www.users.bigpond.com/timo_nieminen/spirits.html&quot;&gt;http://www.users.bigpond.com/timo_nieminen/spirits.html&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
Very true.&lt;br&gt;
&lt;br&gt;
Please don't get me wrong, bye the way ... In spite of my&lt;br&gt;
comment you make some excellent points in your post.  This&lt;br&gt;
&quot;what is i&quot;? question is a philosophical one and reasonable&lt;br&gt;
points about good coding practice can be made on both sides.</description>
    </item>
    <item>
      <pubDate>Mon, 21 Apr 2008 02:40:11 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427872</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Dave Bell&quot; &amp;lt;dbell@TheSPAMFREEBells.net&amp;gt; wrote in message &lt;br&gt;
news:u8xOj.1371$26.1046@newssvr23.news.prodigy.net...&lt;br&gt;
&amp;gt; Rex wrote:&lt;br&gt;
&amp;gt;&amp;gt; On Apr 19, 7:23 pm, dpb &amp;lt;n...@non.net&amp;gt; wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; I assume you've read the section in the online documentation on&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; &quot;Optimizing Matlab Code&quot; in the m-file programming section?&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; --&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; Yes, I have; that is the type of resource I am looking for. However, I&lt;br&gt;
&amp;gt;&amp;gt; want to be clear that I'm not just looking for tips on optimizing the&lt;br&gt;
&amp;gt;&amp;gt; performance of my code;  I also want to learn to write more elegant&lt;br&gt;
&amp;gt;&amp;gt; and concise code.&lt;br&gt;
&amp;gt;&amp;gt; Rex&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; That was a good example of what I encountered as a new Matlab user, as &lt;br&gt;
&amp;gt; well. It's an entirely different programming paradigm, and it takes a &lt;br&gt;
&amp;gt; while to get used to. (I still program Matlab like C!)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Another issue I ran into was simply searching for help. When I was trying &lt;br&gt;
&amp;gt; to speed up a project I was working on, someone suggested using benchmark &lt;br&gt;
&amp;gt; before and after changing some code. Not wanting to seem dumber than I &lt;br&gt;
&amp;gt; was, I didn't ask how to benchmark my code, I just figure I'd look it up. &lt;br&gt;
&amp;gt; Try it - there is no such command. One of the two hits I got from 'Help &lt;br&gt;
&amp;gt; benchmark' was an article on training a neural net, the other was about a &lt;br&gt;
&amp;gt; Simulink model of a Physics 1 experiment with a cart and mass. I won't &lt;br&gt;
&amp;gt; admit to how long it took to find 'bench'!&lt;br&gt;
&lt;br&gt;
Actually, I think what you want is the Profiler:&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f9-17018.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f9-17018.html&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/profile.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/profile.html&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
If you encounter a situation like this in the future, where searching the &lt;br&gt;
documentation or the website using your keyworks doesn't find the &lt;br&gt;
information you want, please let our documentation staff know what you &lt;br&gt;
searched for and what you expected to find with those keywords.  On most if &lt;br&gt;
not all of the documentation pages on the website, there's a link in the &lt;br&gt;
upper-right corner of the frame showing the doc page that allows you to &lt;br&gt;
provide feedback.  This will let our documentation staff make it easier to &lt;br&gt;
find that information.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
    <item>
      <pubDate>Mon, 21 Apr 2008 15:20:08 -0400</pubDate>
      <title>Re: How to be a good Matlab programmer</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/167906#427988</link>
      <author>Rex</author>
      <description>On Apr 20, 10:40 pm, &quot;Steven Lord&quot; &amp;lt;sl...@mathworks.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; &quot;Dave Bell&quot; &amp;lt;db...@TheSPAMFREEBells.net&amp;gt; wrote in message&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; news:u8xOj.1371$26.1046@newssvr23.news.prodigy.net...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Rex wrote:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; On Apr 19, 7:23 pm, dpb &amp;lt;n...@non.net&amp;gt; wrote:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;&amp;gt; I assume you've read the section in the online documentation on&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;&amp;gt; &quot;Optimizing Matlab Code&quot; in the m-file programming section?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;&amp;gt; --&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; Yes, I have; that is the type of resource I am looking for. However, I&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; want to be clear that I'm not just looking for tips on optimizing the&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; performance of my code;  I also want to learn to write more elegant&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; and concise code.&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; Rex&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; That was a good example of what I encountered as a new Matlab user, as&lt;br&gt;
&amp;gt; &amp;gt; well. It's an entirely different programming paradigm, and it takes a&lt;br&gt;
&amp;gt; &amp;gt; while to get used to. (I still program Matlab like C!)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Another issue I ran into was simply searching for help. When I was trying&lt;br&gt;
&amp;gt; &amp;gt; to speed up a project I was working on, someone suggested using benchmark&lt;br&gt;
&amp;gt; &amp;gt; before and after changing some code. Not wanting to seem dumber than I&lt;br&gt;
&amp;gt; &amp;gt; was, I didn't ask how to benchmark my code, I just figure I'd look it up.&lt;br&gt;
&amp;gt; &amp;gt; Try it - there is no such command. One of the two hits I got from 'Help&lt;br&gt;
&amp;gt; &amp;gt; benchmark' was an article on training a neural net, the other was about a&lt;br&gt;
&amp;gt; &amp;gt; Simulink model of a Physics 1 experiment with a cart and mass. I won't&lt;br&gt;
&amp;gt; &amp;gt; admit to how long it took to find 'bench'!&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Actually, I think what you want is the Profiler:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f9-1...&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_env/f9-1...&lt;/a&gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; &lt;a href=&quot;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/profile.html&quot;&gt;http://www.mathworks.com/access/helpdesk/help/techdoc/ref/profile.html&lt;/a&gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; If you encounter a situation like this in the future, where searching the&lt;br&gt;
&amp;gt; documentation or the website using your keyworks doesn't find the&lt;br&gt;
&amp;gt; information you want, please let our documentation staff know what you&lt;br&gt;
&amp;gt; searched for and what you expected to find with those keywords.  On most if&lt;br&gt;
&amp;gt; not all of the documentation pages on the website, there's a link in the&lt;br&gt;
&amp;gt; upper-right corner of the frame showing the doc page that allows you to&lt;br&gt;
&amp;gt; provide feedback.  This will let our documentation staff make it easier to&lt;br&gt;
&amp;gt; find that information.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; --&lt;br&gt;
&amp;gt; Steve Lord&lt;br&gt;
&amp;gt; sl...@mathworks.com&lt;br&gt;
&lt;br&gt;
Thanks for the replies, everyone! Those were really helpful and useful.</description>
    </item>
  </channel>
</rss>

