필터 지우기
필터 지우기

How to save cumsum values in loop depending on conditions

조회 수: 1 (최근 30일)
Amanda S
Amanda S 2020년 6월 6일
편집: Amanda S 2020년 6월 6일
I have some troubles with saving cumsum-arrays depending on a condition. I now have a code that goes through a list of values (the column with total demands from the data bellow), sums the values together from top to bottom and starts over the cumsum when the threshould of 37 is reached. For example the list of 14 14 12 12 6 6 is becoming 14 28 [break] 12 24 30 36 [break]. But i want to modify the code to only merge/ count the cumsum depending on conditions being met.
An example for what I want to achieve: 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. But I don't know how and where in the code to apply this.
A snippet of the data (there's a lot more data in the big model) is the following:
Cust. 1 Cust. 2 Demand 1 Demand 2 Total demand
12 10 4 10 14 I want to save this merge in an array (customer 12+10) so that
array A is customer 12 + 10 with the demand 14
10 12 10 4 14 Check if any of the customer already are saved in an array, and
since they are (ArrayA), skip this row value and
continue to check the next row.
11 10 2 10 12 Check the same thing as above, since 10 is in array A, check if
11 can also be merged in array A as long as the
demand isn't exceeding the threshold of 37. Since 14 + 2 = 16,
11 is ok to merge in array A and check next row.
If it wasn't ok, skip this merge/row and continue to the next row
value to check it.
10 11 10 2 12 Continue to check...
12 11 4 2 6 Continue to check...
8 6 6 2 8 Since none of 8 or 6 is part of any saved array and the demand
is smaller than the threshold, save this merge in
a new array B and check next row...
I now have the code:
x = Sum_efterfr_SavingList_kol6; % the "total demand" column
threshold = 37;
totalSum = cumsum(x);
index = 1;
count = 1; % final count will be total number of partition in the array
Loopdata = {};
while index <= length(x)
newIndex = find(totalSum>threshold,1); % First index in the array where value > threshold
if isempty(newIndex)
Loopdata{count} = cumsum(x(index:end)); % for last index take the full array till the end
break;
else
% Take the array upto newIndex - 1 because value exceeds threshold at newIndex
Loopdata{count} = cumsum(x(index:newIndex-1));
totalSum = totalSum - totalSum(newIndex-1); % Subtracting the current accumulated value so that next time the index start from newIndex
index = newIndex;
count = count + 1;
end
end
celldisp(Loopdata)
Many thanks for any help on the way!!

답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by