Anonymous Function - how to impose limits
Show older comments
How to impose max/min limits on anonymous functions i.e. I have to ensure that the current cell does not go beyond this matrix pTransition i.e. bounds are [-2,-2] and [2,2]
Thanks
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);
% above are the given conditions
%trying to implement the foll function:
calcPTransition = @(x_t, u_t, x_tm1)...
pTransition(x_t(1) + 3, x_t(2) + 3);
10 Comments
John Chilleri
on 13 Jan 2017
Would you mind posting the entire code? I believe that with careful definition of some variables, you can achieve your goal.
Ken
on 13 Jan 2017
Edited: Walter Roberson
on 13 Jan 2017
John Chilleri
on 13 Jan 2017
I see that you add 3 to local coordinates because you're 'translating' [-2,2] to [1,5].
Are your x_t, x_tm1, and u_t also [-2,2], because if they're already [1,5] that would be your problem.
Also, you don't appear to be using u_t in the function.
Lastly, Walter Roberson posted an answer below in case you didn't see it - it might be what you needed.
Image Analyst
on 13 Jan 2017
Why not just simply make a regular function? It would only be a few lines and would be far, far less cryptic and hard to read. What's the big deal?
Ken
on 16 Jan 2017
Ken
on 16 Jan 2017
Image Analyst
on 16 Jan 2017
Edited: Image Analyst
on 16 Jan 2017
What are you trying to impose limits on? The input or the output? And I don't see why u_t & x_tm1 are being passed in since the function uses x_t only.
pTransition(x_t(1) + 3, x_t(2) + 3);
What is the point of passing in u_t & x_tm1?
Let's say you have an inputMax, inputMin, and outputMax and outputMin. You could limit both input(s) and outputs to those min and max values, but I'm not sure what you want. Do you want to do that? Or you want to clip just the inputs, or just the outputs only? Please clarify.
Ken
on 16 Jan 2017
Ken
on 17 Jan 2017
Accepted Answer
More Answers (2)
Walter Roberson
on 13 Jan 2017
If you mean in the sense that if the index is out of bounds then instead the closest in-bounds index should be used, then:
BoundsIndex2D = @(M,r,c) M( max( min(r, size(M,1), 1 ), max( min(c, size(M,2), 1 );
calcPTransition = @(x_t, u_t, x_tm1) BoundsIndex2D(pTransition, x_t(1) + 3, x_t(2) + 3);
However, in the cases where you might want something like this, such as running a minimization where the probe location is to index an array, then typically the indices would end up not being constrained to integers, and then you would have the difficulty that you are trying to index at a fractional location. In the cases where you can be sure that the indices are integer you can typically also be sure that the indices will be in range.
13 Comments
Ken
on 13 Jan 2017
Walter Roberson
on 13 Jan 2017
BoundsIndex2D = @(M,r,c) M( max( min(r, size(M,1)), 1 ), max( min(c, size(M,2)), 1) );
Ken
on 13 Jan 2017
Walter Roberson
on 13 Jan 2017
Please check your bracket positions. I had them wrong before but the version I posted most recently corrected them.
Ken
on 14 Jan 2017
Ken
on 15 Jan 2017
Walter Roberson
on 15 Jan 2017
Ensuring that the indices do not go beyond bounds is a quite different matter than calculating for a specific purpose. My adjustment makes sure that you do not get any out of bounds error by instead accessing the location closest to the border. Depending on how the function is to be used, it could be that what you want to do is not suitable for an anonymous function.
Or in some situations a way around the problem might be to return 0 instead of an actual value, perhaps by using
BoundsIndex2D = @(M,r,c) (r >= 1 & r <= size(M,1) & c >=1 & c <= size(M,2)) .* M( max( min(r, size(M,1)), 1 ), max( min(c, size(M,2)), 1) );
Ken
on 16 Jan 2017
Walter Roberson
on 16 Jan 2017
Well, you can follow what they suggest there of using max() and min() .
Walter Roberson
on 16 Jan 2017
min(max([-2, -2], x_t-(x_tm1+u_t)), [2,2])
Ken
on 16 Jan 2017
Edited: Walter Roberson
on 17 Jan 2017
Eva Barceló Michans
on 4 Aug 2020
Can someone help me please?
Is regarding the same exercise Ken share.
I put this code:
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);
%%%%%%%%%%%PLEASE DON'T CHANGE ANYTHIN..
x_t = [0,0];
x_tm1 = [0,0];
u_t = [0,0];
BoundsIndex2D = @(M,r,c)min(max([-2, -2], x_t-(x_tm1+u_t)), [2,2])
calcPTransition = @(x_t, u_t, x_tm1) BoundsIndex2D...
(pTransition, x_t(1) + 3, x_t(2) + 3);
plotPredictionUpdate(calcPTransition);
The error Matlab is:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.
I don't know how to solve it.
Thank you
2 Comments
Image Analyst
on 4 Aug 2020
Instead of using anonymous functions, just make them regular functions, which are SO much easier to debug.
Eva Barceló Michans
on 6 Aug 2020
The problem is that I am doing an online course and I have to do it with anonymous function.
Categories
Find more on Matrix Indexing 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!