Anonymous Function - how to impose limits
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ken
il 13 Gen 2017
Commentato: Eva Barceló Michans
il 6 Ago 2020
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
Risposta accettata
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
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?
Più risposte (2)
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
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
il 4 Ago 2020
Instead of using anonymous functions, just make them regular functions, which are SO much easier to debug.
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.
Vedere anche
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!