sum of different columns to the desired value in Matlab

5 次查看(过去 30 天)
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 个评论
Mohammad Sami
Mohammad Sami 2022-8-5
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 2022-8-5
Column A values are given. And I need to get the sum of BCDE as per the particular Column A value in each row.

请先登录,再进行评论。

采纳的回答

Bruno Luong
Bruno Luong 2022-8-5
编辑:Bruno Luong 2022-8-5
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 个评论
jack carter
jack carter 2022-8-5
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 2022-8-5
编辑:Bruno Luong 2022-8-5
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

请先登录,再进行评论。

更多回答(1 个)

Bruno Luong
Bruno Luong 2022-8-5
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 个评论
jack carter
jack carter 2022-8-5
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Calendar 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by