|
In article <fq1lcs$t07$1@atlantis.news.tpi.pl>,
Kamil <milka-wywal@to.free.of.pl> wrote:
>I am wondering, if there was a simple way to suppress 'for' command in
>MATLAB.
>This would force students to use smarter solutions than just a lot of
>for loops and waiting :)
Sometimes using a 'for' or 'while' loop *is* the most efficient way.
For example a couple of weeks ago, I had a case in which the
standard matlab method of writing a section of code was roughly
[X,Y] = ndgrid(A,B);
answer = sum(X < Y);
Now allow A and B to grow to roughly 1200 elements each, and
call this code 100,000 times, and these two lines of code took
approximately 26.13 minutes to execute.
After some thought, I replaced this short code with a 'while'
loop that took noticably more coding lines, but was linear in the
size of A (and independant of the size of B). With the same dataset,
calling the same 100,000 times, the replacement code took 2.39 seconds
total to execute.
Now, if I were to turn in that 'while' solution that was ~50 times
faster than anyone else achieved using matlab vectorized operations,
would you fail me on the assignment for not using a sufficiently
"smart" solution?
--
"The beauties of conception are always superior to those of
expression." -- Walter J. Phillips
|