Finding the output of this command
1 次查看(过去 30 天)
显示 更早的评论
The variable 'state' is a 60 x 2 matrix.
xdiv = (0.55-(-1.5))/10;
x = -1.5:xdiv:0.5;
What does the command
[d s] = min(dist(statelist,x'))
do?
This is a part of the function that is labeled 'discretized states'. Originally the state x is continuous in the interval [-1.5, 0.55].
0 个评论
回答(1 个)
Walter Roberson
2015-12-20
dist() is a distance function. For each state in statelist, the distance to all of the x is computed. Then the min() finds the minimum of those distances, and so is finding the closest x to each state in the statelist. The d is returning how far it is from an x and the s is returning the index of the x.
Except that it constructs the x slightly wrong due to floating point roundoff. It would be better if it had used
x = linspace(-1.5, 0.55, 11);
Does the code actually need the distance from the nearest x? Or does it just need to know which is the nearest x? If it just needs to know which is the nearest x then
nearest_x = round((statelist-(-1.5))/xdiv) * xdiv + (-1.5);
2 个评论
Walter Roberson
2015-12-20
A function that just needs to discretize over a uniformly spaced array can use the nearest_x that I showed.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!