removing for loop by using cellfun

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

Daniel Shub
Daniel Shub 2012년 4월 30일
Why do you want to remove the loop? In general I find arrayfun and cellfun difficult to read, understand, and maintain.
Richard
Richard 2012년 4월 30일
The loop takes a long time to run. When I use arrayfun for this particular problem it runs faster that by just using a loop. Therefore, I wanted to see the speed of using a combination of cellfun and arrayfun.
Daniel Shub
Daniel Shub 2012년 4월 30일
I am not sure if arrayfun is much faster than a loop with proper preallocation.
Oleg Komarov
Oleg Komarov 2012년 4월 30일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 4월 30일

0 개 추천

Data = cellfun(@(x,y)arrayfun(@(jj)mean(x(jj,1:y(jj))),1:length(y)),temp,bin,'un',0);

추가 답변 (0개)

카테고리

도움말 센터 및 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!

Translated by