using arrayfun for a function with multiple inputs

I want to call my Before function with a 1 x 40 structure called struct for the s input, a value of 0 for the begins input, and a value of 20 for the ends input, but I can't figure out how to make it work with arrayfun correctly.
Here's what I have now, and the error that it gives me is at the bottom:
x = arrayfun(@Before, [struct,0,20]);
function v = Before(s,begins,ends)
Numbers = s.H;
firstColumn = Numbers(:,1);
rowsUnder = (firstColumn>begins)&(firstColumn<ends);
sNumNew = Numbers(rowsUnder,:);
v = mean(sNumNew(:,3));
end
Error using horzcat
The following error occurred
converting from double to
struct:
Error using struct
Conversion to struct from double
is not possible.
Error in ComparingMeans (line 22)
x = arrayfun(@Before,[struct,0,20]);

 채택된 답변

Matt J
Matt J 2014년 1월 26일
편집: Matt J 2014년 1월 26일
Multiple arrayfun arguments should not be concatenated. You need to do
x = arrayfun(@Before, myStructure, 0 , 20);
Since "struct" is also the name of a built-in MATLAB function, you will minimize coding hazards if you use a different name for it, like I did.

댓글 수: 3

oh yes yes I didn't realize that, but now, when I try to run:
x = arrayfun(@Before, myStructure, 0, 20);
it gives me this error:
Error using arrayfun All of the input arguments must be of the same size and shape. Previous inputs had size 43 in dimension 2. Input #3 has size 1.
Error in ComparingMeans (line 22) Mean(1,:) = arrayfun(@BeforeMean, myStructure, 0, 20);
Matt J
Matt J 2014년 1월 26일
편집: Matt J 2014년 1월 26일
Instead of passing scalars for 0 and 20, you'll have to pass vectors of the same length as myStructure
d=size(myStructure);
a=zeros(d);
b=a;
b(:)=20;
x = arrayfun(@Before, myStructure, a, b);
That's very strange. I thought arrayfun did scalar expansion. That might be only for gpuArrays....
ah thanks, it works now

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

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2014년 1월 26일
b1=0;
e1=20;
x = arrayfun(@(x1)Before(x1,b1,e1), your_structure)

댓글 수: 2

Yes, this also works, thanks
this worked for me as well. Thanks

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

카테고리

도움말 센터File Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

태그

질문:

2014년 1월 26일

댓글:

2020년 2월 27일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by