필터 지우기
필터 지우기

Anonymous Function - how to impose limits

조회 수: 9 (최근 30일)
Ken
Ken 2017년 1월 13일
댓글: Eva Barceló Michans 2020년 8월 6일
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
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
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
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by