- It has an input x that is not actually used by the function (x only appears in the @(x) not in the expression itself)
- You're using () to index your cell array instead of {}. () returns a portion of a cell array as a cell array. If you want the content of a particular cell it's ppx{1, 1}
- Even if you'd used {} to get the content of the cell array you'd still be subtracting/multiplying functions instead of numbers.
cell entry-function_handler calculation
1 次查看(过去 30 天)
显示 更早的评论
In matrix 'px' store the x-coordinate of P12, P13.(just points)
px{1} = @(x) x(1); %xp12
px{2} = @(x) x(2); %xp13
And generate the permutation of them.
ppx = perms(px);
But, When I try to calculate them like below:
fun = @(x) (ppx(1,1)-ppx(1,2))*(ppx(2,1)-ppx(2,2));
fminsearch(fun,[0,0]);
-------------- Always the error message occur:
Undefined operator '*' for input arguments of type
'cell'.
Error in
test4>@(x)pp1x(1,1)*pp1x(1,2)-pp1x(2,1)*pp1x(2,2)
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
Error in test4 (line 15)
fminsearch(fun,[0,0]);
Help me..
0 个评论
回答(2 个)
Guillaume
2017-1-24
编辑:Guillaume
2017-1-24
Your first statement: "In matrix 'px' store the x-coordinate of P12, P13.(just points)" is incorrect. Your px is a cell array of anonymous functions, it does not store any coordinates.
You then use perms on that cell array, which surprisingly works despite cell arrays not being documented as an acceptable input. This results in a larger cell array of anonymous function.
You then declare another anonymous function that has many problems:
My guess is that you wanted to do:
fun = @(x) (ppx{1,1}(x) - ppx{1, 2}(x)) * (ppx{2, 1}(x) - ppx{2,2}(x));
possibly, maybe. A clearer version of the above would be:
fun = @(x) (x(2)-x(1)) * (x(1)-x(2))
%aka:
fun = @(x) -(x(2)-x(1))^2
which is not very useful for fminsearch.
Note that the error message shows a different fun (with swapped * and -). It does not make much more sense since the equivalent would be:
fun = @(x) x(1)*x(2) - x(2)*x(1)
%aka
fun = @(x) 0
5 个评论
Stephen23
2017-1-24
p12x = @(x) x(1);
etc.
Why do these have to be function handles? What is wrong with simply generating permutations of x ?
Walter Roberson
2017-1-24
You are assigning using cell notation, which is necessary to store multiple function handles. But then you use () indexing, and the result of () on a cell array is a cell array (possibly a 1x1 cell array), not what is stored in the cell array. And you cannot multiply cell arrays.
If you were to use {} notation then you would get the function handles, but you would have a problem because it is not valid to subtract function handles.
You need to use something like
fun = @(x) (ppx{1,1}(x) -ppx{1,2}(x))*(ppx{2,1}(x)-ppx{2,2}(x));
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!