Is there a way to improve max & min functions in matlab?
8 views (last 30 days)
Show older comments
Hello everyone, I'm using max and min functions on a vector of (just) 2 element in a very very long for cycle, and it is simply too slow. Is there a way to avoid using this functions, or, at least, improve them? Thanks in advance for any answer! And sorry for my english, I'm italian =) !
Answers (1)
dpb
on 18 Jun 2017
Edited: dpb
on 18 Jun 2017
Well, the first question would be can you vectorize the loop and then do the min/max operation over the array result instead of just a 2-vector?
For two values only you can write an explicit expression--one possibility is to assume one, then check--
mxv=v(1); if v(2)>v(1), mxv=v(2); end
or just the obvious if ... else ... end
Ways you can play with logical addressing as well...altho you have to be certain to not end up with a null return if aren't careful in implementation.
I don't know how much it might speed up anything, though it would eliminate the function overhead. I'd've thunk the JIT optimizer might would've inlined that by now, however, unless it can't tell the size of the input. Or mayhaps TMW figured the case wasn't frequent enough to have analyzed for.
ADDENDUM
Meant to put this first, but better late than never... :)
Before you start such micro-optimizing, have you profiled the app to actually locate these as bottlnecks?
4 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!