MATLAB: Anonymous Function – how to impose limits

anonymous functions

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);

Best Answer

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));