Is there any other way of using a loop without the loop command like for, while etc..
4 次查看(过去 30 天)
显示 更早的评论
Hi there, just as the title say, is there any other way? I'm currently runing a programe with take quite a while to execute because of the loop...so just curiuos if anyone know how to do a loop without using the loop command?
Ta, Rak
0 个评论
采纳的回答
Walter Roberson
2011-11-30
You can hide the loop by using arrayfun() or cellfun() or the like, but those are convenience functions that will loop internally.
"for" and "while" are the only two basic repetition structures in MATLAB.
You could construct a recursive function to do the looping implicitly, but that is unlikely to be any faster.
The most common cause of slow loops is a failure to preallocate space for the outputs, which (until 2011b anyhow) results in the new memory having to be continually allocated, old memory copied to new, old memory deallocated. That can be very bad for performance.
"for" loops have not been especially slow for several years, and in modern versions can be faster than vectorizing. It is what is in the body of the for loop that can be slow.
You should consider using the profiler to find out where you program is slow.
1 个评论
Jan
2013-7-21
@francesco: Especially for the FOR loops, which profit form the JIT acceleration, the profiler shows misleading results: The JIT can reorder the sequence of the commands to reduce the run-time. But this would confuse the profiler and therefore the JIT acceleration is automatically disabled when the profiler is started. What a pity, that this reduces the use of the profiler considerably!
Some TIC/TOCs can be more useful then.
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!