Anonymous Function - how to impose limits

5 次查看(过去 30 天)
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 个评论
Ken
Ken 2017-1-16
The input. This is a 5X5 matrix and the intent of limits is to prevent the cells from going outside the matrix. Tried this:
@(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);
but get error:
The return value of calcPTransition must be a scalar double but it is a [1 2] double!
The condition input argument must be a scalar logical.
The return value of calcPTransition must be a scalar double but it is a [1 2] double!
The condition input argument must be a scalar logical.
Ken
Ken 2017-1-17
Thanks for replying.
Did not see the 2nd part of the post from Image Analyst, so replying to it now:
Agree, no need of sending u_t and x_tm1. Only need to limit inputs, outputs will take care of themselves. Just want to clip the inputs at -2,-2 on the min and 2,2 on the max, so that I don't go outside the matrix. The matrix centre is at 3,3 hence the limits -2,-2 and 2,2.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2017-1-17
See if this is what you want:
function test()
% Call GetPLocalTransition with -2, 99 (both outside)
calcPTransition = GetPLocalTransition(-2, 99);
% Call GetPLocalTransition with 2, 4 (both inside)
calcPTransition = GetPLocalTransition(2, 4);
% Call GetPLocalTransition with 2, 15 (row inside, column outside)
calcPTransition = GetPLocalTransition(2, 15);
% Row and col can be anything. If outside the edges of pTransition,
% then they will be clipped to the edge location
function output = GetPLocalTransition(row, col)
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 ];
[rows, columns] = size(pTransition);
% Clip to being inside the array.
if row < 1
fprintf('Row of %d clipped to row #1\n', row);
row = 1;
end
if col < 1
fprintf('Column of %d clipped to column #1\n', col);
col = 1;
end
if row > rows
fprintf('Row of %d clipped to row #%d\n', row, rows);
row = rows;
end
if col > columns
fprintf('Column of %d clipped to column #%d\n', col, columns);
col = columns;
end
% Row and col are definitely inside now.
output = pTransition(row, col);
fprintf('pTransition(%d, %d) = %.2f\n', row, col, pTransition(row, col));
  3 个评论
Image Analyst
Image Analyst 2017-1-17
Can't be. I just copied and pasted that from my test.m and it worked fine. You must have changed something.
And who is this "they" that you are talking about? I thought you needed it for yourself. Why would your question be stated verbatim in a "problem"? It's not homework, or else you would have tagged it as homework. Nor did you say it was homework anywhere I remember. So who is "they"? Your employer??? Why would they impose a one line requirement instead of just getting the job done regardless of how many lines?
Ken
Ken 2017-1-17
Tried it in Matlab - fine. This is a problem I attempted from an Auto. Mobile Robots course.
Thanks a lot!

请先登录,再进行评论。

更多回答(2 个)

Walter Roberson
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
Ken 2017-1-16
编辑:Ken 2017-1-16
Tried but error:
Could not eval "calcPTransition([0, 0], [0, 0], [0, 0])" : Undefined function or variable 'x_t'.
Undefined function or variable 'x_t'.
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..
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);
Ken
Ken 2017-1-16
编辑:Walter Roberson 2017-1-17
Since I was getting errors, I init'ed:
x_t = [0,0];
x_tm1 = [0,0];
then added the 2 lines:
@(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);
but get error:
The return value of calcPTransition must be a scalar double but it is a [1 2] double!
The condition input argument must be a scalar logical.
u_t = [0,0];

请先登录,再进行评论。


Eva Barceló Michans
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
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
The problem is that I am doing an online course and I have to do it with anonymous function.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by