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!