Sort an array while keeping the right index

3 views (last 30 days)
aHi!
My problem is that I want to "fill" the biggest factory with as many customers demand as possible and when that factory is full, I want to proceed to the next factory and so on.. The goal is to end up with the approximately (!) lowest cost
I have an idea of sorting the arrays like this to solve the problem but is it possible to sort s & d while keeping the right index in f & c?
Before
m = 3; %Number of possible locations
n = 5; %Number of customers
s = [ 225 184 239 ] ; %factory storage limit
d = [ 92 82 83 69 74 ] ; %demand each customer
f = [ 886 766 589]; % fixed cost
c = [ 9 22 26 5 21 %transportation cost from each factory to each customer
16 11 23 28 24
14 5 6 24 6 ];
After
m = 3;
n = 5;
s = [ 239 225 184 ] ; %decending
d = [ 69 74 82 83 92 ] ; %ascending
f = [ 589 766 886 ];
c = [ 14 5 6 24 6
9 22 26 5 21
16 11 23 28 24 ];
Or is there another smarter way? Should I instead try to take the highest/lowest value (something like max(s) and min(d) ?, but then again how to I do when I in the next step need the "second highest/lowest" value?
(Yes, it is a school assignment, I just want guidelines)

Answers (1)

Steven Lord
Steven Lord on 22 May 2019
Look at the second output from the sort function. That can be used to generate the first output of sort from the input array, but it is not limited to being used only on that input array.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!