Anonymous Function - how to impose limits
이전 댓글 표시
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
John Chilleri
2017년 1월 13일
Would you mind posting the entire code? I believe that with careful definition of some variables, you can achieve your goal.
Ken
2017년 1월 13일
편집: Walter Roberson
2017년 1월 13일
John Chilleri
2017년 1월 13일
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
2017년 1월 13일
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
2017년 1월 16일
Ken
2017년 1월 16일
Image Analyst
2017년 1월 16일
편집: Image Analyst
2017년 1월 16일
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
2017년 1월 16일
Ken
2017년 1월 17일
채택된 답변
추가 답변 (2개)
Walter Roberson
2017년 1월 13일
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
Ken
2017년 1월 13일
Walter Roberson
2017년 1월 13일
BoundsIndex2D = @(M,r,c) M( max( min(r, size(M,1)), 1 ), max( min(c, size(M,2)), 1) );
Ken
2017년 1월 13일
Walter Roberson
2017년 1월 13일
Please check your bracket positions. I had them wrong before but the version I posted most recently corrected them.
Ken
2017년 1월 14일
Ken
2017년 1월 15일
Walter Roberson
2017년 1월 15일
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
2017년 1월 16일
Walter Roberson
2017년 1월 16일
Well, you can follow what they suggest there of using max() and min() .
Walter Roberson
2017년 1월 16일
min(max([-2, -2], x_t-(x_tm1+u_t)), [2,2])
Ken
2017년 1월 16일
편집: Walter Roberson
2017년 1월 17일
Eva Barceló Michans
2020년 8월 4일
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
Image Analyst
2020년 8월 4일
Instead of using anonymous functions, just make them regular functions, which are SO much easier to debug.
Eva Barceló Michans
2020년 8월 6일
The problem is that I am doing an online course and I have to do it with anonymous function.
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!