function_handler matrix

2 ビュー (過去 30 日間)
Hyeyun Yang
Hyeyun Yang 2017 年 1 月 25 日
回答済み: Andrei Bobrov 2017 年 1 月 25 日
for i = 1:1:3
px(i) = @(x) x(i);
py(i) = @(x) x(i+3);
end
Then the error occurs:
Error using horzcat
Nonscalar arrays of function handles are not allowed; use
cell arrays instead.
So when I change the code:
for i = 1:1:3
px{i} = @(x) x(i); % cell array
py{i} = @(x) x(i+3);
end
But, in this case, I can not calculate with the cell entries:
fun = @(x)((px{1}-px{2}).^2+(py{1}-py{2}).^2).^.5;
fminsearch(fun, [0,0,0,0]);
Then error occurs:
Undefined operator '-' for input arguments of type
'function_handle'.
Error in kequal_4sub2>@(x)((px{1}-px{2}).^2+(py{1}-py{2}).^2).^.5
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
Error in kequal_4sub2 (line 7)
fminsearch(myfun, [0,0,0,0])
How can I make it possible to calculate with the cell entries?

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 1 月 25 日
fun = @(x)((px{1}(x)-px{2}(x)).^2+(py{1}(x)-py{2}(x)).^2).^.5;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by