Matlab code to calculate minimum distance

In summary: smallest_total = total; smallest_index = i;end%enter the total for this switch into the total cable lengths array%switch_sizes = [switch_sizes; total];%return the co-ordinates of the switch with the lowest value in the array of total cable lengths%%check syntax%%not sure whether he wants co-ordinates or the index, returning index as it says in the brief but check%return smallest_index;
  • #1
mralam
11
0
hey guys,

i want to wirte a function which takes as its arguments two lists of co-ordinates (the first
is a list of customers and the second for possible switch locations), and determines the index of the best switch location to minimise the amount of cable required.

I have done the following:

Code:
%Get the number of customers%
NC = input('Enter the number of costumers there is: ');
customers = [];

%Get each customers co-ordinates and place them into an array%
for i = 1:NC
%could also possibly get input as x y ..
   x = input('Enter the x value of costumer %.1d : ', i);
   y = input('Enter the y value of costumer %.1d : ', i);
   %Array to hold customers%
   customers = [customers;x y];
end

%Get the number of switches%
NS = input('Enter the number of switches there is: ');

switches=[];

%Get each switchs co-ordinates and place them into an array%
for i = 1:NS
   %could also possibly get input as x y ..
   x = input('Enter the x value of switch %.1d : ', i);
   y = input('Enter the y value of switch %.1d : ', i);
   %Array to hold customers%
   switches = [switches;x y];
end

%Create an array with the same size as the number of switches to hold the total cable lengths%
switch_sizes=[];

%do two jobs at once - find the lengths and keep track of the smallest one and the index of the smallest%
smallest_total = 0;
smallest_index = 0;

%For each switch%
for i = 1:NS
  %get co-ords of the switch we are looking at%
  xs = switches(i,1);
  ys = switches(i,2); 
  %Set total to zero%
  total = 0;
  %For each customer%
  for j = 1:NC
    %Calculate the distance between the customer and the switch%
    x= customers(j,1);
    y= customers(j,2);
    distance = sqrt(((x - xs)^2)+((y-ys)^2));
    %TODO%
    %Add it to the total for this switch%
    total = total + distance;
  end

  if (smallest_total > total) 
    smallest_total = total;
    smallest_index = i;
  end

  %enter the total for this switch into the total cable lengths array%
  switch_sizes = [switch_sizes; total];
end

%Return the co-ordinates of the switch with the lowest value in the array of total cable lengths%
%check syntax%
%not sure whether he wants co-ordinates or the index, returning index as it says in the brief but check%
return smallest_index; 
%alternative return switches(smallest_index); %

this code keeps coming back with errors that i am not able figure out how to solve it.
Any advise on how to go about this?

thanks
 
Physics news on Phys.org
  • #2
I ran the code you posted in matlab, and it seems that where you have x=input(...) is the problem. You're treating input as a printf statement, but apparently it doesn't work like that.
Anyway, I modified your code:

Code:
%Get the number of customers%
function retval = blah()
NC = input('Enter the number of costumers there is: ');
customers = [];

%Get each customers co-ordinates and place them into an array%
for i = 1:NC
%could also possibly get input as x y ..
   mystr = sprintf('Enter the x value of costumer %i : ',i); 
   x = input(mystr);
   mystr = sprintf('Enter the y value of costumer %i : ',i); 
   y = input(mystr);
   %Array to hold customers%
   customers = [customers;x y];
end

%Get the number of switches%
NS = input('Enter the number of switches there is: ');

switches=[];

%Get each switchs co-ordinates and place them into an array%
for i = 1:NS
   %could also possibly get input as x y ..
   mystr = sprintf('Enter the x value of switch %i : ',i); 
   x = input(mystr);
   mystr = sprintf('Enter the y value of switch %i : ',i); 
   y = input(mystr); 
   %Array to hold customers%
   switches = [switches;x y];
end

%Create an array with the same size as the number of switches to hold the total cable lengths%
switch_sizes=[];

%do two jobs at once - find the lengths and keep track of the smallest one and the index of the smallest%
smallest_total = 0;
smallest_index = 0;

%For each switch%
for i = 1:NS
  %get co-ords of the switch we are looking at%
  xs = switches(i,1);
  ys = switches(i,2); 
  %Set total to zero%
  total = 0;
  %For each customer%
  for j = 1:NC
    %Calculate the distance between the customer and the switch%
    x= customers(j,1);
    y= customers(j,2);
    distance = sqrt(((x - xs)^2)+((y-ys)^2));
    %TODO%
    %Add it to the total for this switch%
    total = total + distance;
  end

  if (smallest_total > total) 
    smallest_total = total;
    smallest_index = i;
  end

  %enter the total for this switch into the total cable lengths array%
  switch_sizes = [switch_sizes; total];
end

%Return the co-ordinates of the switch with the lowest value in the array of total cable lengths%
%check syntax%
%not sure whether he wants co-ordinates or the index, returning index as it says in the brief but check%
retval = smallest_index; 
%alternative return switches(smallest_index); %
end

I tested it, and it runs, although I always get the number 0 as a return value. :)
 
  • #3
i have tested the code that you have posted and it worked but the result is alos 0, do you have any ideas on what can be the problem?
 
  • #4
Well, I wasn't checking your algorithm for *what it's supposed to do*, but rather for getting rid of errors due to miscalled functions. Just try outputting values at each point of your algorithm and compare the answer with what you expect you should get. So, take a small number of people, small number of switches, a pen and piece of paper, and calculate what intermediate values you should be getting. Compare this to what your program is telling you and you'll work out where the error is.

Anyway, on brief inspection, you have:

Code:
smallest_total = 0;
...
total = 0;
...
distance = sqrt(((x - xs)^2)+((y-ys)^2));
...
total = total + distance;
...
if (smallest_total > total) 
...

Here, smallest_total=0. Also, distance>0, so that total>0. Your "if" statement at the bottom of the code then requires that (smallest_total > total) to execute the code inside the "if" block. This means you require that 0>x, where x>0. So this bit of your code inside the "if" block never gets touched.
 
  • #5
Thanks for your help i got this to work now.
 
  • #6
hi! friends my name is sagar
i'm new here..well I'm in the last year of Mechatronics engineering
my project is size control of parle G biscuit with the help of MATLAB using camera..

i hav created a program that finds the edge of tht even i hav also reach to the finding the external edge by removing internal edge

finally i want to find the distance between to that the first pixel in row and last pixel of the same row and go from first row to last row as above

pls help me out how to do that
my program is as shown below:

clear all
clc

a=imread('parleg.jpg');
b=rgb2gray(a);

% b = .2900*a(:,:,1)...
% +.5870*a(:,:,2)...
% +.1140*a(:,:,3);
% min(b(:));
% ans=0;
% max(b(:));
% ans=255;
% colormap(gray(256));

c=edge(b,'canny');

[m,n]=size(c);
e=zeros(m,n);
minx=zeros(m);
maxx=zeros(m);

for i=1:m
d=0;
for j=1:n
if(c(i,j)==1 & d==0)
minx(i)=j;
% miny(i)=j;
d=1;
elseif(c(i,j)==1 & d==1)
maxx(i)=j;
%maxy(i)=j;
end
end
end

for i=1:m
if(minx(i)~=0)
for k=minx(i):maxx(i)
e(i,k)=1;
%break;
end
end
end


figure
subplot(2,2,1);
imshow(a);
subplot(2,2,2);
imshow(b);
subplot(2,2,3);
imshow(c);
subplot 224
imshow(e);

figure
imshow(c);
 
  • #7
Hey guys,
I need help. Say i take a picture of a dot in a frame followed by another picture whereby the dot has moved to another spot in the frame. How do I calculate the distance between these 2 dots? At the end, i would have 2 jpeg images of the dot at 2 different locations. What I'm trying to do is calculate how far the dot has moved. Any help with the MATLAB codes would be very much appreciated. Thanks!

Jeremy
 
  • #8
How to retrieve last value in an array type number field and calculate any number functions on this field?

Lets say my field is "http://www.mindfiresolutions.com/How-to-retrieve-last-value-in-an-array-type-number-field-and-calculate-it-448.php" [Broken]" number type and repetition is 6 in table Dashboard and I have entered values to that field as 1,3,2,5,8,4
Then calculation Last (Dashboard::TEST) will return value 4

Max (Dashboard::TEST ) will return 8
Min ( Dashboard::TEST ) will return 1
Sum (Dashboard::TEST) will return 23
 
Last edited by a moderator:

1. What is Matlab code used for?

Matlab code is used for mathematical and scientific computation, data analysis, and visualization. It is commonly used in fields such as engineering, physics, and finance.

2. How can I calculate the minimum distance between two points using Matlab?

To calculate the minimum distance between two points using Matlab, you can use the built-in function "pdist". This function takes in a matrix of coordinates and returns a vector of distances between each pair of points. You can then use the "min" function to find the minimum distance in the vector.

3. Can Matlab calculate the minimum distance between a point and a line?

Yes, Matlab has functions that can calculate the minimum distance between a point and a line. One way to do this is by using the "distpointline" function, which takes in the coordinates of the point and the line and returns the minimum distance.

4. Is there a way to calculate the minimum distance between multiple sets of points using Matlab?

Yes, Matlab has functions that can calculate the minimum distance between multiple sets of points. One way to do this is by using the "pdist2" function, which takes in two matrices of coordinates and returns a matrix of distances between each pair of points.

5. Can I visualize the minimum distance calculated using Matlab?

Yes, Matlab has built-in functions for data visualization. You can use the "plot" function to plot the points and lines, and the "scatter" function to visualize the minimum distance between points. You can also use the "imshow" function to display the distances as an image.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
29
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
982
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
886
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
914
Back
Top