using arrayfun for a function with multiple inputs

7 ビュー (過去 30 日間)
Michael
Michael 2014 年 1 月 26 日
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 件のコメント
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....
Michael
Michael 2014 年 1 月 26 日
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 件のコメント
Michael
Michael 2014 年 1 月 27 日
Yes, this also works, thanks
Anik Hirenkumar Shah
Anik Hirenkumar Shah 2020 年 2 月 27 日
this worked for me as well. Thanks

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by