Ranking Variables by Value
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Maria Hart
el 27 de Mayo de 2020
Comentada: Jeff Miller
el 28 de Mayo de 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
0 comentarios
Respuesta aceptada
Jeff Miller
el 27 de Mayo de 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 comentarios
Jeff Miller
el 28 de Mayo de 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.
Más respuestas (1)
Steven Lord
el 27 de Mayo de 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)
Ver también
Categorías
Más información sobre Startup and Shutdown en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!