When to break the MATLAB rules?

조회 수: 2 (최근 30일)
Doug Hull
Doug Hull 2011년 5월 12일
I am planning a new video for http://blogs.mathworks.com/videos. It will be about when to "break the rules".
I am looking for more examples like the following.
Two of the examples that I want to cover are:
  • Vectorize
I have seen some tortured logic in the name of vectorizing code. These cases could easily be replaced with a clear, readable for loop. The coder gained nothing in speed with this, yet lost readability.
Vectorizing is the right thing to do in many cases, taking advantage of MATLAB's matrix centric view of the world. It is not always the right thing though. Since the JIT (Just In Time) compiler was introduced, the former "for loop penalty" is largely gone. Vectorizing is not always important for speed now.
  • Pre-allocate vectors in for loops
For large for loops, this is the right thing to do, avoiding the churn of an ever growing matrix. However, I saw a case today where someone was doing a lot of tedious, unclear, calculations to figure out how big a matrix would be at the end of a for loop (it was not simply growing by one each time through). In the end, it looked like the matrix was never going to get bigger than 200x200. The time saved at run time was trivial. MATLAB is a dynamicaly typed language such that you do not have to worry about this kind of thing.
There are times to break these rules for readability, ease of coding, because of only marginal improvements in performance vs code complexity. Can you think of other times when "breaking the rules" is the right thing to do?

답변 (4개)

James Tursa
James Tursa 2011년 5월 12일
In-place operations in mex routines when the variables are huge and you can't afford the extra copies that MATLAB would normally make in doing an operation. (It would be nice if TMW would supply some official API functions to detect sharing status etc to make this job easier for the mex programmer to avoid unwanted side effects.)

Matt Fig
Matt Fig 2011년 5월 12일
One example where breaking the rules applies is the use of dynamic pre-allocation, as discussed and demonstrated in my (and Jan's) post here.
  댓글 수: 1
Andrew Newell
Andrew Newell 2011년 5월 12일
Now I can vote for it a second time!

댓글을 달려면 로그인하십시오.


Matt Fig
Matt Fig 2011년 5월 12일
I have seen good uses for EVAL. One that comes to mind is Doug Shwarz's BSXFUN replacement.
  댓글 수: 1
Jan
Jan 2011년 5월 14일
I think, EVAL is obligatory, if you want to access a GLOBAL variable, whose name is determined dynamically, because "global(VarName)" does not work, but "eval(['global ', VarName])" does. The same for PERSISTENT.
But let me mention, that such contructs are bad programming methods in my opinion.

댓글을 달려면 로그인하십시오.


Sean de Wolski
Sean de Wolski 2011년 5월 12일
When trying to write code to solve some type of image processing problem, I'll start with I, then I1, then I1opened, I2, I2dilated, I3, I3nosmalljunk, I4, IstuffIcareaboutHopefully, I5

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by