Finding the output of this command

1 view (last 30 days)
cgo
cgo on 20 Dec 2015
Commented: Walter Roberson on 20 Dec 2015
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].

Answers (1)

Walter Roberson
Walter Roberson on 20 Dec 2015
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 Comments
cgo
cgo on 20 Dec 2015
I am not actually sure if the 'dist' function is present in previous versions of Matlab. I am using 2013b now and when I first ran the program, there was an error on the 'dist' function. I thought it was quite strange since euclidean distance seems to be very basic to not have it.
Anyway, I searched in this forum the meaning of 'dist' and found this:
function z2 = dist(w, p)
% dist function similar to neural network toolbox
S = size(w,1);
Q = size(p,2);
z2 = zeros(S,Q);
if (Q<S)
p = p';
copies = zeros(1,S);
for q=1:Q
z2(:,q) = sum((w-p(q+copies,:)).^2,2);
end
else
w = w';
copies = zeros(1,Q);
for i=1:S
z2(i,:) = sum((w(:,i+copies)-p).^2,1);
end
end
z2 = sqrt(z2);
I couldn't make out if it is exactly the same or what. I am unsure about it now.
Walter Roberson
Walter Roberson on 20 Dec 2015
A function that just needs to discretize over a uniformly spaced array can use the nearest_x that I showed.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!