Hey guys i have to do the following:(adsbygoogle = window.adsbygoogle || []).push({});

we have n customers and m switches, and each switch can accomodate a maximum of c customers (where c > n=m). Write a function which uses a Greedy Algorithm to determine

which customers should be connected to which switch. it needs to generalise from two switches to m switches.

The algorithm that needs to be used is the following:

where NC is the number of costumersCode (Text):

L = 0.75*NC;

for i = 1:NC

for j=1:NS

for i = 1:NC

if distance(i,1) < distance(i,2)

if |S1| < L

S1 = S1 + {i}

else

S2 = S2 + {i}

end if

else

if |S2| < L then

S2 = S2 + {i}

else

S1 = S1 + {i}

end if

end if

end

NS is the number of switches

S1 and S2 are the set of costumers connected to switch 1 and 2 respectively

I have done the following code:

but its not doing the job. Any suggestions on how i can fix this code up so it does what i need?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

S1=[]; %S1 is the set of costumers connected to switch 1

S2=[]; %S2 is the set of costumers connected to switch 2

%Get the maximum number of costumers that the switch can accomandate

C = input('Enter maximum number of costumers that the switch can accomandate: ');

if C < (NC/NS)

frpintf('THis switch cannot be used')

else

%the following algorithm is for 2 switches only

L = 0.75*NC;

for i = 1:NC

for j=1:NS

for i = 1:NC

if distance(i,1) < distance(i,2)

if |S1| < L

S1 = S1 + {i}

else

S2 = S2 + {i}

end if

else

if |S2| < L then

S2 = S2 + {i}

else

S1 = S1 + {i}

end if

end if

end

thanks

**Physics Forums - The Fusion of Science and Community**

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# MATLAB costumers and switches

Can you offer guidance or do you also need help?

Draft saved
Draft deleted

Loading...

Similar Threads for MATLAB costumers switches | Date |
---|---|

MATLAB MATLAB: forces acting on an object | Apr 6, 2018 |

MATLAB Matlab histogram | Apr 4, 2018 |

MATLAB Sovling Bernoulli's differential equation in matlab? | Apr 3, 2018 |

MATLAB Matlab-like software | Mar 27, 2018 |

Mathematica How to use Switch with variable number of terms? | Mar 18, 2017 |

**Physics Forums - The Fusion of Science and Community**