- 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 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Guillaume
on 24 Jan 2017
Edited: Guillaume
on 24 Jan 2017
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 Comments
Stephen23
on 24 Jan 2017
p12x = @(x) x(1);
etc.
Why do these have to be function handles? What is wrong with simply generating permutations of x ?
Walter Roberson
on 24 Jan 2017
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 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!