Comments on: Cleaner code in MATLAB part one of series https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/?s_tid=feedtopost Stuart uses video to share his experiences solving problems with MATLAB day-to-day, interesting new features, plus tips and tricks he has picked up along the way. Tue, 19 May 2009 18:22:19 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Nasser https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-214 Wed, 05 Dec 2007 12:05:26 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-214 Hi sebaa,

Have u found a NSGA-II coded in matlab with binary code

]]>
By: Nasser https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-213 Wed, 05 Dec 2007 11:14:21 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-213 Thank indeed for ur help but I’m looking for NSGA-II with binary coded (representation) the one that have u sent if real vector

]]>
By: Doug https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-212 Mon, 11 Jun 2007 12:28:04 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-212 File exchange to the rescue:

https://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectId=10429&objectType=File

]]>
By: sebaa https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-211 Sat, 09 Jun 2007 12:26:20 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-211 dear sir
do you have a NSGA-II coded in matlab with binary code

thanks

]]>
By: DanK https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-210 Thu, 21 Dec 2006 14:14:30 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-210 Loren,
Thanks for the answer. From that response it does sound like the complicated, ugly, code probably does save a lot of memory allocation, at least when you are dealing with large sets of data.

As a side note, it would be interesting if profiler had a mechanism which allowed one to know what part of an expression was using time, rather than just line by line. I’m guessing that that is a far from simple modification, however.
Dan

]]>
By: Loren Shure https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-209 Wed, 20 Dec 2006 18:23:06 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-209 Dan-

With respect to temporary expressions, MATLAB cleans up or reuses the memory as it goes. So, for example, assuming all variables and sizes are suitable for this expression

(a+b)+(c+d)

We end up with 2 intermediate temporaries (not 3). The temporaries start off being for the expressions in parentheses. And when those subexpressions are added, MATLAB is smart enough to know that it can overwrite one of the temps with the final value. So at most here, you have only one extra temporary).

There are some extra considerations that MATLAB does for expressions like

A = A + 1;

when this is inside a function M-file. I will be writing about that soon in one of my upcoming blog articles.

–Loren

]]>
By: DanK https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-206 Wed, 20 Dec 2006 17:13:58 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-206 Doug,
I’m thinking mainly of the extra time required in memory allocation and handling, when you are processing large volumes of data. What I am really unsure of here, is what memory usage MatLab makes for intermediate steps in complex computations. Is it allocating the memory just the same as it would if we assigned each intermediate value to a variable and then clearing that allocation at the end of the computation? If so then I would imagine you really don’t lose much time. For reference I’m thinking about processing done on arrays that are, for example 20,000×25, which can definitely chew up a bunch of memory.

Dan

]]>
By: Doug https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-208 Wed, 20 Dec 2006 14:29:02 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-208 Dan and Nick,

There are times when cryptic code code does give faster results. The profiler, as outlined in an earlier video helps to diagnose that. However, I think that sloppy code, like the kind in my original file, is rarely faster, simply harder to understand. Even if the clearer code were slower, the ease of understanding would outweigh most small performance gains in most situations.

That being said, I think it is worth adding another video at the end to compare the two. My instinct is that their will be negligible difference in speed.

Doug

]]>
By: DanK https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-207 Mon, 18 Dec 2006 18:14:17 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-207 I have the same question as Nick. My solution has generally been to write the complicated code, and then provide the “simple” code as a comment, so that I don’t have to give up speed. I also wonder about the merits of cluttering up the variable name space… Is this yet another case, “there is never really one size that fits all”?

]]>
By: Nick Cheilakos https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-205 Sun, 17 Dec 2006 04:37:25 +0000 https://blogs.mathworks.com/videos/2006/12/13/cleaner-code-in-matlab-part-1-of-series/#comment-205 We have a mess code but is efficient an we have a clean code but loses in speed. Which code we prefer?

In your example which code is faster? The first or the clean version?

ps: I know, that if we have clean code we can do debugging very easy.But i dont know if sometimes we earn something else

]]>