While Loop Help

3 vues (au cours des 30 derniers jours)
Cote
Cote le 29 Avr 2011
The Assignment:
The problem I'm having is that I can't figure out how to set up my loop to get it to keep the weight under 60 tons while using the highest value to weight ratios. I know I should be using a while loop and most likely a break but the rest is confusing me .This is my code so far:
function Relief_Shipment()
a = load('data1.txt');
b = zeros(length(a),4);
%ID Number
b(:,1) = a(:,1);
%Weight (in tons)
b(:,2) = a(:,2);
%Value to the disaster victims
b(:,3) = a(:,3);
%Value/Weight Ratio
b(:,4) = a(:,3)./a(:,2)
%Sorting the value/weight ratio from the smallest value to highest
B = sort(b(:,4))
while ?
%I know I must create a condition so that it stays under 60 tons but I don't know how to incorporate my value/weight ratio
end
end
Any suggestions or hints would be very helpful. Thanks for your time.

Réponse acceptée

random09983492
random09983492 le 29 Avr 2011
I assume you will add items to the ship based solely on value/weight ratio?
If so, this is quite simple. A while loop is written like this:
while(condition)
%Operations to do while the condition is met.
end
So obviously, the condition of the while loop would be that your total weight is less than 60 tons (you want to add items until the first time your total weight is over 60 tons). Of course this means you will have to create a check to remove the last item added if the weight exceeds 60 tons.
Here is some pseudocode to help you:
declare variable for total weight
declare counting variable (i)
while the total weight is less than 60:
add weight of B(i) to shipment
increment i by 1
end
There is an error I see with your current though. You only sorted the value/weight column, making it impossible to get the correct ID numbers. Check this article to see how to sort this better.
  1 commentaire
Cote
Cote le 29 Avr 2011
Thanks a ton that was very informative, and I'll look into sorting the other columns

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by