sum of different columns to the desired value in Matlab

1 visualizzazione (ultimi 30 giorni)
I have list of numbers from Row 1 to Row 20 say in Column A and I need to have the number of each row distributed in to four different columns (Column B, C, D, E) in Matlab so that:
(i) the sum of values shown in columns B, C, D, E is equal to the value of Column A.
(ii) The values shall only be whole numbers.
(iii) Columns B, C, D, E have maximum limit of 10, 10, 5 and 5 respectively. That is the values generated in these columns should be with in these maximum limit values.
Example:
| A | B | C | D |
| 16 | 6 | 5 | 2 | 3 |
Any way to pick values from Column A and then get the values in different columns (B,C,D,E) with in their respective limits so that the sum is equal to A?
Sample Column A values are as under:
16
17
20
26
18
29
19
20
18
16
16
21
16
16
17
16
28
29
20
25
16
Previously for another similar project I tried doing this with limits of 3 and 5. But here I need to get values from A and then do the summation within limits.
clear all
while true
A=randi([3 5],20,4);
if((sum(A,2)>=42) | (sum(A,2)<=70));
break;
end
end
disp(A);
disp(sum(A,2));
  2 Commenti
Mohammad Sami
Mohammad Sami il 5 Ago 2022
If the column is not given but is generated. You can just generate the values for BCDE and then sum it up to get the value for A
jack carter
jack carter il 5 Ago 2022
Column A values are given. And I need to get the sum of BCDE as per the particular Column A value in each row.

Accedi per commentare.

Risposta accettata

Bruno Luong
Bruno Luong il 5 Ago 2022
Modificato: Bruno Luong il 5 Ago 2022
A=randi([4 30],10,1);
lo = [1 1 1 1];
up = [10 10 5 5];
BCDE=lo+diff(round((cumsum([0, (up-lo)],2)).*(A-sum(lo))./sum(up-lo)),1,2); % EDIT bug fix
if any(BCDE < lo | BCDE > up)
error('Fail')
end
[A,BCDE]
ans = 10×5
19 6 6 4 3 10 3 3 2 2 26 9 8 5 4 19 6 6 4 3 10 3 3 2 2 16 5 5 3 3 4 1 1 1 1 11 3 4 2 2 8 2 3 1 2 27 9 9 4 5
  2 Commenti
jack carter
jack carter il 5 Ago 2022
Thanks alot !!! Working fine.
Just an optional addition if possible to have a lower limit values of BCDE 5,5,3,3, respectively as well.
Bruno Luong
Bruno Luong il 5 Ago 2022
Modificato: Bruno Luong il 5 Ago 2022
Hmm just change it at your will
A=randi([16 30],10,1);
lo = [5 5 3 3];
up = [10 10 5 5];
BCDE=lo+diff(round((cumsum([0, (up-lo)],2)).*(A-sum(lo))./sum(up-lo)),1,2); % EDIT bug fix
if any(BCDE < lo | BCDE > up)
error('Fail')
end
[A,BCDE]
ans = 10×5
29 10 9 5 5 25 8 8 5 4 29 10 9 5 5 27 9 9 4 5 26 9 8 5 4 29 10 9 5 5 25 8 8 5 4 21 7 7 3 4 17 5 6 3 3 24 8 8 4 4

Accedi per commentare.

Più risposte (1)

Bruno Luong
Bruno Luong il 5 Ago 2022
If you have the optimization toolbox
A=randi([4 30],10,1);
n = length(A);
lo = [1 1 1 1];
up = [10 10 5 5];
m = length(up);
BCDE = zeros(n,m);
opts = optimoptions('intlinprog','Display','off');
for k=1:n
BCDE(k,:) = intlinprog(zeros(1,m),1:m,...
[],[],ones(1,m),A(k),lo,up,[],opts);
end
[A(:) BCDE]
ans = 10×5
6 3 1 1 1 19 10 7 1 1 13 10 1 1 1 25 10 10 4 1 13 10 1 1 1 17 10 5 1 1 17 10 5 1 1 25 10 10 4 1 22 10 10 1 1 10 7 1 1 1
  4 Commenti
jack carter
jack carter il 5 Ago 2022
I do not have the Optimization toolbox, so I cannot run the code. I guessed from the randi function that the values are generated randomly

Accedi per commentare.

Categorie

Scopri di più su Dates and Time in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by