Anonymous Function - how to impose limits

10 visualizzazioni (ultimi 30 giorni)
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 Commenti
Ken
Ken il 16 Gen 2017
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 il 17 Gen 2017
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.

Accedi per commentare.

Risposta accettata

Image Analyst
Image Analyst il 17 Gen 2017
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 Commenti
Image Analyst
Image Analyst il 17 Gen 2017
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 il 17 Gen 2017
Tried it in Matlab - fine. This is a problem I attempted from an Auto. Mobile Robots course.
Thanks a lot!

Accedi per commentare.

Più risposte (2)

Walter Roberson
Walter Roberson il 13 Gen 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 Commenti
Ken
Ken il 16 Gen 2017
Modificato: Ken il 16 Gen 2017
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 il 16 Gen 2017
Modificato: Walter Roberson il 17 Gen 2017
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];

Accedi per commentare.


Eva Barceló Michans
Eva Barceló Michans il 4 Ago 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 Commenti
Image Analyst
Image Analyst il 4 Ago 2020
Instead of using anonymous functions, just make them regular functions, which are SO much easier to debug.
Eva Barceló Michans
Eva Barceló Michans il 6 Ago 2020
The problem is that I am doing an online course and I have to do it with anonymous function.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by