removing for loop by using cellfun
Show older comments
Could anyone provide information on how to remove the for loop from this line of script:
for i =1:length(Name);
Data{i} = arrayfun(@(j)mean(temp{i}(j,1:bin{i(j))),1:length(bin{i}),'un',1);
end
I have tried:
Data = arrayfun(@(j)cellfun(@(a,b)mean(a(j,1:b(j))),1:lenth(b),'un',1),temp,bin,'un',0);
An error returns:
Too many input arguments.
How would I remove the loop?
4 Comments
Daniel Shub
on 30 Apr 2012
Why do you want to remove the loop? In general I find arrayfun and cellfun difficult to read, understand, and maintain.
Richard
on 30 Apr 2012
Daniel Shub
on 30 Apr 2012
I am not sure if arrayfun is much faster than a loop with proper preallocation.
Oleg Komarov
on 30 Apr 2012
I think a for loop should be faster and definitely more readable in this case.
However, 1:bin{i(j))) has a typo and i(j) doesn't make sense since "i" is always a scalar.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!