Ranking Variables by Value

11 vues (au cours des 30 derniers jours)
Maria Hart
Maria Hart le 27 Mai 2020
Commenté : Jeff Miller le 28 Mai 2020
Dear all,
let's assume I have a basket of 4 different kinds of fruits (Apples, Oranges, Bananas, Pears).
n_a
n_o
n_b
n_p
tell me the number (n) of each fruit that I have.
If my basket is full, I would like to sell the rest of the fruits that I do not need. However, every fruit has a different value (price = p). Let's say
p_a = 4
p_o = 2
p_b = 1
p_p = 0
Let's say, I have x too many fruits, so
toomanyfruits = x
Now, I would first like to sell the ones, I get the most value from (I can only sell as many as I have). If toomanyfruits is still > 0, I would like to sell the ones which are second most valuable etc. pp..
So, I tried to use the command sort, but I have the problem that I do not need the sorted values, but the names of that fruit which has the highest value to be able to then call it for ranking. I tried some different things, but do not seem to get the right thing.
Some help would be highly appreciated :-)
With kind regards
Maria

Réponse acceptée

Jeff Miller
Jeff Miller le 27 Mai 2020
You might find the second output of the sort command useful. E.g.
fruitVals = [p_a p_b p_o p_p];
[~, idxOrder] = sort(fruitVals);
Now idxOrder(1) will be the location (1-4) in fruitVals of the lowest-price fruit, idxOrder(2) the second-lowest, and so on.
  2 commentaires
Maria Hart
Maria Hart le 27 Mai 2020
Modifié(e) : Maria Hart le 27 Mai 2020
Hello Jeff, thank you very much for your quick reply
I have thought about this function as well. However, I am more stuck on the next step where I call the firstly ranked entry, then the one ranked second if there are still excess fruits in the basket.
if fruitVals(1) = 'p_a'
toomanyfruits_apple = min([n_a,toomanyfruits],[],2);
toomanyfruits = toomanyfruits - toomanyfruits_apple
elseif fruitVals(1) = 'p_b'
...
end
If I continue with an if-function it gets rather complicated and I am not quite sure how to call the one that is ranked second.
I am sorry, I guess, I have not clearly stated my problem.
Jeff Miller
Jeff Miller le 28 Mai 2020
Steven is right that you would be much better off setting this up with arrays. They will be harder to set up initially, but then everything else will be much easier (e.g., you can avoid these nasty sequences of if statements).
Think of every fruit as having a number that reflects its position in an array like fruitNames. Also, the order of the fruits in this array is completely irrelevant. Then, after the sort command, idxOrder(end) will tell you the position number of the most expensive fruit (the ones you want to sell first), idxOrder(end-1) will tell you the position number of the second most expensive fruit (the ones you want to sell second), and so on.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 27 Mai 2020
You've made this a bit harder on yourself than it needs to be by defining individual variables for the counts and prices of each fruit. Instead I would define three variables.
fruitCounts = randi([0 5], 1, 4) % Random numbers (between 0 and 5) of each type of fruit
fruitPrices = randi([0 5], 1, 4) % Random prices (between 0 and 5) of each type of fruit
fruitNames = ["apple", "orange", "banana", "pear"] % Fruit names
Now when you sort fruitPrices as Jeff Miller suggested, you can directly see what type of fruit that is and how much of it you have.
[sortedPrices, priceOrder] = sort(fruitPrices);
sortedPrices
fruitCounts(priceOrder)
fruitNames(priceOrder)
  1 commentaire
Maria Hart
Maria Hart le 27 Mai 2020
Hello Mr. Lord,
thank you very much for your answer. Unfortunately, this is not possible. This is a part of a bigger problem (I am designing an App in AppDesigner) where the number of fruits are defined by various functions that depend on local circumstances (temperature, weather etc. pp.) which is different for every fruit, so n_p etc. is actually the output from individual functions. The prices can be defined in AppDesigner individually as well and will vary depending on what the user inserts. Maybe, I should have stated that, but I wanted to bring the fruit example to explain the problem as easy as possible. If you feel, I should delete or edit the post and write the whole story, please, let me know, I am a newbie to this.
I further think that my problem does not lie on sorting the variables, but rather on the subsequent step:
"Now, I would first like to sell the ones, I get the most value from (I can only sell as many as I have). If toomanyfruits is still > 0, I would like to sell the ones which are second most valuable etc. pp.. "
I have tried this with the if-command-lines, however it gets quite complicated. Maybe my solution lies in some kind of loops?
With kind regards
Maria

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by