cell entry-function_handler calculation

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..

回答(2 个)

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:
  • 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.
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 个评论

As you said, the cell array does not store any coordinate, so I try to store the coordinate in the matrix like:
px(1) = @(x) x(1);
px(2) = @(x) x(2);
Then also the error message occur:
Conversion to cell from function_handle is not possible.
Error in test4 (line 9)
p1x(i) = @(x) x(i);
Then... what am I supposed to do..
Rather than us telling you what's wrong each time, why don't you tell us what it is you want to do?
I want to generate the permutation of the coordinates of the points:
perms1 = p13 p12
p12 p13
And the p12, p13 points are the variables:
p12x = @(x) x(1);
p13x = @(x) x(2);
p12y = @(x) x(3);
p13y = @(x) x(4);
And I want to make a function to calculate the distance between the points I set.
sx = 0.0123;
sy = 0.231;
tx = 0.0321;
ty = 0.121;
fun1 = |sp12|+|p12p13|+|p12t|
fminsearch(fun,[0,0])
fun2 = |sp13|+|p13p12|+|p13t|
fminsearch(fun,[0,0])
when I write the fun1,fun2 by directly using the "x(1)" and "x(2)", it works.
But it is exhausted when the number of points are larger. So I want to do it by for loop, also I supposed to use the permutation matrix having the "function_handler" as a entry.
p12x = @(x) x(1);
etc.
Why do these have to be function handles? What is wrong with simply generating permutations of x ?

请先登录,再进行评论。

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));

类别

帮助中心File Exchange 中查找有关 Operators and Elementary Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by