How to use anonymous functions

5 次查看(过去 30 天)
Ken
Ken 2022-3-5
评论: Ken 2022-3-12
I am trying to understand anonymous functions. What exactly does this do:
pTransition = [
0 0 0 0 0
0 .05 .1 .05 0
0 .1 .4 .1 0
0 .05 .1 .05 0
0 0 0 0 0 ];
getPLocalTransition = @(localCoordinate) ...
pTransition(localCoordinate(1) + 3, localCoordinate(2) + 3);
  1 个评论
Walter Roberson
Walter Roberson 2022-3-5
What exactly does this do
We do not know exactly what it does. The details of how function handles are created and processed internally are MATLAB proprietary details, hidden in code that people outside Mathworks do not have access to.
I could probably spend more than an hour writing up and providing references to how it probably works internally, in order to be consistent with actions we can test and documentation we have access to, but unless you are into Compiler Construction, and Denotation Semantics, that would likely be a waste of my time, compared to the possibility that you could instead ask a specific question that you are trying to resolve in your mind.
When you get into an elevator and you ask "How exactly does this work?" are you asking about multi-phase motor theory, or are you asking about the material science behind the precise choice of compounds used in the emergency brakes, or are you asking about how the emergency phone works, or are you asking about how capacitance sensing works for the push buttons?

请先登录,再进行评论。

采纳的回答

KSSV
KSSV 2022-3-5
The function needs a localCoordinate as input and it should be a 1X2 array. The array can have values 0,1,2.
Depedning on the values given, to the array 3 is added, these are the indices and the respective values are extracted from pTransition.
  74 个评论
Ken
Ken 2022-3-12
Got a response:
"You now have 2 issues left, the first one is about the MATLAB function syntax: When you define a function with function dir = get_direction(x_t, u_t, x_tm1), the variable dir is what gets returned in the end, so your code has always returned [0,0] so far.
The second is about the vector that you are calculating. The matrix pTransition describes the probability to move onto a neighbouring cell of x_tm1+u_t, further getPLocalTransition(...) expects a direction vector that is relative to this location. Since you use u_t+x_tm1 as input, which is relative to wold origin (0,0), this will most likely result to a probability of 0 (because the map is much bigger than the 5x5 of pTransition)."
Ken
Ken 2022-3-12
I tried this i.e. did not use dir=[0;0]. Then I used the foll sentence to convert to local coordinates. Still no luck. getPLocalTransition(pTransition(localCoordinate(1) + 3, localCoordinate(2) + 3)); function dir = get_direction(x_t, u_t, x_tm1); temp = u_t + x_tm1 ; dir = getPLocalTransition(max(-2,min(2,temp))); dir= getPLocalTransition(pTransition(localCoordinate(1) + 3, localCoordinate(2) + 3)); end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Connectivity to ROS-Enabled Simulators 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by