MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   MATLAB技术文章 (https://www.labfans.com/bbs/forumdisplay.php?f=25)
-   -   Which Way to Compute: cellfun or for-loop? (https://www.labfans.com/bbs/showthread.php?t=21985)

poster 2019-11-23 20:41

Which Way to Compute: cellfun or for-loop?
 
[B] [URL="https://blogs.mathworks.com/loren/2019/09/25/which-way-to-compute-cellfun-or-for-loop/"]Which Way to Compute: cellfun or for-loop?[/URL] [URL="https://blogs.mathworks.com/loren/2019/09/25/which-way-to-compute-cellfun-or-for-loop/#view_comments"]9[/URL][/B]

Posted by [B][URL="https://www.mathworks.com/matlabcentral/profile/authors/136425-loren-shure?s_tid=blg_to_profile"]Loren Shure[/URL][/B], September 25, 2019

Over a long number of years, people have asked whether it's better to use [URL="https://www.mathworks.com/help/matlab/ref/cellfun.html"]cellfun[/URL] or a [URL="https://www.mathworks.com/help/matlab/ref/for.html"]for-loop[/URL] to perform certain computations. As with so many things in life, the correct answer here is "it depends". Let me give you a bit more insight so you can make good choices.

[B]Contents[/B]
[LIST][*][URL="https://blogs.mathworks.com/loren/2019/09/25/which-way-to-compute-cellfun-or-for-loop/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#8000febb-dee5-4de0-b384-db09f40537b3"]Function Handles[/URL][*][URL="https://blogs.mathworks.com/loren/2019/09/25/which-way-to-compute-cellfun-or-for-loop/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#7f6c2bd3-e54f-4225-92bb-5e118a0f7c3c"]A Selected Set of Functions[/URL][*][URL="https://blogs.mathworks.com/loren/2019/09/25/which-way-to-compute-cellfun-or-for-loop/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+mathworks%2Floren+%28Loren+on+the+Art+of+MATLAB%29#2eb63826-9af9-4292-9f33-30cba0ec2980"]Your Use of cellfun[/URL][/LIST][B]Function Handles[/B]

If the function handle being called in cellfun takes a long time to evaluate, then the overhead from cellfun will be insignificant. So no big deal to use cellfun in this case.

If the function being called in cellfun is very fast, such as issparse, you will likely be in the situation where the function call overhead dominates. Then use the function directly in a for-loop.

If I know the number of inputs and outputs and the function I want to apply elementwise, I generally write an explicit loop. That might not be your preference, however. Remember, you can always put the code you wanted to write into comments!

[B]A Selected Set of Functions[/B]

When cellfun was originally written, it could handle a fixed set of character arrays as the first input, with no additional flexibility. Over the years, the functionality has advanced. If you [B]do[/B] use one of the sanctioned fix character arrays (and now scalar strings), however, you will get performance equivalent to the for-loop version.

Here are the strings you can supply as the first input to cellfun and expect high performance (from the Backward Compatibility section in the doc for cellfun:
[LIST][*]"isempty"[*]"islogical"[*]"isreal"[*]"length"[*]"ndims"[*]"prodofsize"[*]"size"[*]"isclass"[/LIST]There are some caveats with using these strings, including
[LIST][*]cellfun does not call any overloaded versions of these functions[*]cellfun('isclass',C,classname) returns logical 1 (true) for each element of C that matches the classname argument. This syntax returns logical 0 (false) for objects that are a subclass of classname[/LIST][B]Your Use of cellfun[/B]

Do you use cellfun? When and how? With what first arguments? Let us know [URL="https://blogs.mathworks.com/loren/?p=3448#respond"]here[/URL].


Get the MATLAB code

Published with MATLAB® R2019b


[url=http://feedproxy.google.com/~r/mathworks/loren/~3/x45c6YG4zrk/]更多...[/url]


所有时间均为北京时间。现在的时间是 23:22

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.