How to use anonymous functions

2 Ansichten (letzte 30 Tage)
Ken
Ken am 5 Mär. 2022
Kommentiert: Ken am 12 Mär. 2022
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 Kommentar
Walter Roberson
Walter Roberson am 5 Mär. 2022
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?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

KSSV
KSSV am 5 Mär. 2022
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 Kommentare
Ken
Ken am 12 Mär. 2022
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 am 12 Mär. 2022
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Encryption / Cryptography finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by