Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab code to calculate minimum distance

  1. Apr 3, 2008 #1
    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 (Text):

    %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
     
  2. jcsd
  3. Apr 4, 2008 #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 (Text):

    %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. :)
     
  4. Apr 4, 2008 #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?
     
  5. Apr 4, 2008 #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 (Text):

    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.
     
  6. Apr 5, 2008 #5
    Thanks for your help i got this to work now.
     
  7. Jan 29, 2010 #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);
     
  8. Mar 24, 2010 #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
     
  9. Apr 23, 2010 #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 "TEST" 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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Matlab code to calculate minimum distance
  1. Matlab codes (Replies: 2)

  2. Matlab code (Replies: 0)

Loading...