Anonymous Function - how to impose limits
4 次查看(过去 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);
采纳的回答
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
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?
更多回答(2 个)
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 个评论
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
2020-8-4
Instead of using anonymous functions, just make them regular functions, which are SO much easier to debug.
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 Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!