How to save while loop values in multiple arrays of different length to merge if condition if met

2 views (last 30 days)
Hi,
I have this code at the moment that loops through a list of values (customer demands from 2 customers being merged) from top to bottom and sums the values ​​into an accumulated sum, breaks the accumulated sum when a specific threshold is reached (37) and then starts over the accumulated sum from the next number in the list. For example, the list of 14 14 12 12 6 6 12 12 20 is becoming 14 28 [break] 12 24 30 36 [break] 12 24 [break] 20 [break].
x = Sum_efterfr_SavingList_kol6; % the list of values
threshold = 37;
index=1;
while 1
xc = cumsum(x(index:end));
m = find(xc > thresh,1,'first');
if isempty(m)
break
end
x(m) = x(m)-xc(m-1);
Loopdata{m}=xc; % this is not saving the values that i want, only where in the list that the cumsum is starting over
end
My first problem is partly that I want to save each accumulated sum into individual arrays instead of a long list, for example I want A = (14 28), B = (12 24 30 36) and so on, but I can't get it to work properly.
But I also can't figure out how (and where in the while loop) to implement that I only want to use the cumsum on the values of customers that isn't already part of a saved array, because I aim to eventually execute the Clarke & Wright algoritm with merging customers in tours (the saved arrays will eventually be the tours) and the list with cumsums will not loop from top to bottom.
Would really appreciate some help!

Accepted Answer

Raunak Gupta
Raunak Gupta on 30 May 2020
Hi,
Below code might help you. Here I am finding the first index in cumulative sum where the value is greater than threshold and then taking the array upto that point. For next time I am subtracting the value accumulated till the current index so that next time we will start from next index in array and doing the same process until the end of array.
Hope you will understand the code.
x = randi(15,[20,1]);
threshold = 37;
totalSum = cumsum(x);
index = 1;
% final count will be total number of partition in the array
count = 1;
Loopdata = {};
while index <= length(x)
% First index in the array where value > threshold
newIndex = find(totalSum>threshold,1);
if isempty(newIndex)
% for last index take the full array till the end
Loopdata{count} = cumsum(x(index:end));
break;
else
% Take the array upto newIndex - 1 because value exceeds threshold
% at newIndex
% Here predefining Loopdata can be handy but we don't know the size
Loopdata{count} = cumsum(x(index:newIndex-1));
% Subtracting the current accumulated value so that next time the
% index start from newIndex
totalSum = totalSum - totalSum(newIndex-1);
index = newIndex;
count = count + 1;
end
end
  5 Comments
Amanda S
Amanda S on 31 May 2020
Is it possible in an easy way, to somehow modify the code to only merge values in the cumsum depending on a condition? For example: if customer 12 and 10 have the total demand of 14 (4+10) and are in a tour together saved in an array, while customer 11 and 10 have the total demand of 12 (2+10). I want to chech if customer 12, 10, 11 can all be merged together, since 11 could be merged with 10 but 10 is already merged with 12. If it is possible to merge 11 with 12&10, add the value of customer 11 to the 12 and 10 array and the cumsum in that array. If it is not possible to merge the three of them, skip that value and continue to check if the next value in the list can be added.
So instead of just merging the values in a list continuously from top to bottom and break when the threshold is reached, I want to check if the vales actually can be merged with regard to what has already been merged and saved.
Is my question making any sence?
% CUSTOMER CUSTOMER DEMAND DEMAND TOTAL DEMAND
% 1 2 CUS1 CUS2 CUS1+CUS2
------------------------------------------------------
12 10 4 10 14
10 12 10 4 14
11 10 2 10 12
10 11 10 2 12
12 11 4 2 6
11 12 2 4 6
11 5 2 10 12
5 11 10 2 12
10 5 10 10 20
5 10 10 10 20
12 5 4 10 14
5 12 10 4 14
8 6 6 2 8
6 8 2 6 8
6 5 2 10 12
8 5 6 10 16
5 6 10 2 12
5 8 10 6 16

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!