[英]Is there an algorithm to average using different constraints on data?
Month![]() |
Product![]() |
Product![]() |
Product![]() |
Current![]() |
---|---|---|---|---|
1 ![]() |
Y![]() |
A![]() |
17 ![]() |
|
2 ![]() |
Y![]() |
A![]() |
22 ![]() |
|
3 ![]() |
Y![]() |
A![]() |
21 ![]() |
|
4 ![]() |
Y![]() |
S![]() |
Q![]() |
44 ![]() |
5 ![]() |
Y![]() |
S![]() |
Q![]() |
32 ![]() |
6 ![]() |
Y![]() |
S![]() |
Q![]() |
23 ![]() |
7 ![]() |
Y![]() |
S![]() |
B![]() |
56 ![]() |
8 ![]() |
Y![]() |
S![]() |
B![]() |
73 ![]() |
9 ![]() |
Y![]() |
S![]() |
B![]() |
12 ![]() |
10 ![]() |
Y![]() |
C ![]() |
26 ![]() |
|
11 ![]() |
Y![]() |
C ![]() |
45 ![]() |
|
12 ![]() |
Y![]() |
C ![]() |
37 ![]() |
As seen in table above I have some data, elements of which form part of different products.如上表所示,我有一些数据,其中的元素构成了不同产品的一部分。 For example, 4th element in above table (highlighted row) ie,
44
is part of product Q
, S
and Y
.例如,上表中的第 4 个元素(突出显示的行)即
44
是乘积Q
、 S
和Y
。 Each product has an average as below :每个产品的平均值如下:
average of Product A=(17+22+21)/3=20
average of Product Q=(44+32+23)/3=33
average of Product S=(44+32+23+56+73+12)/6=40
average of Product Y=(17+22+21+44+32+23+56+73+12+26+45+37)/12=34
Now I want to manipulate the data such that new averages are:现在我想操纵数据,使新的平均值是:
Average of product A= 25
Average of product Q = 35
Average of Product S= 32
Average of product Y= 39
and shape of months within a product should be maintained.应保持产品内的月份形状。
So result should be (example calculation):所以结果应该是(示例计算):
new Month 1= (new average - current average) +current month 1 value
new Month 1= (25 - 20) + 17 = 22
new Month 2= (new average - current average) +current month 2 value
new month 2 = (25 - 20 ) + 22 = 27
new Month 3= (new average - current average) +current month 3 value
new months 3 =(25-20)+ 21= 26
monthly shape difference example月形差示例
month![]() |
month 1![]() |
month 2![]() |
month 3![]() |
---|---|---|---|
current![]() |
17 ![]() |
22 ![]() |
21 ![]() |
current monthly difference![]() |
22-17= 5 ![]() |
21-22= -1 ![]() |
|
New![]() |
22 ![]() |
27 ![]() |
26 ![]() |
New monthly difference![]() |
27-22= 5 ![]() |
26-27= -1 ![]() |
It can see that month 2 - month1
for both current and new data is 5
and month 3- month 2
is -1
for both current and new data.可以看到当前数据和新数据的第
month 2 - month1
是5
而当前数据和新数据的5
month 3- month 2
是-1
。 Hence, monthly shape for product A
is maintained.因此,产品
A
月度形状保持不变。
Month![]() |
Product![]() |
Product![]() |
Product![]() |
New![]() |
---|---|---|---|---|
1 ![]() |
Y![]() |
A![]() |
22 ![]() |
|
2 ![]() |
Y![]() |
A![]() |
27 ![]() |
|
3 ![]() |
Y![]() |
A![]() |
26 ![]() |
|
4 ![]() |
Y![]() |
S![]() |
Q![]() |
46 ![]() |
5 ![]() |
Y![]() |
S![]() |
Q![]() |
34 ![]() |
6 ![]() |
Y![]() |
S![]() |
Q![]() |
25 ![]() |
7 ![]() |
Y![]() |
S![]() |
B![]() |
38 ![]() |
8 ![]() |
Y![]() |
S![]() |
B![]() |
55 ![]() |
9 ![]() |
Y![]() |
S![]() |
B![]() |
-6 ![]() |
10 ![]() |
Y![]() |
C ![]() |
57 ![]() |
|
11 ![]() |
Y![]() |
C ![]() |
76 ![]() |
|
12 ![]() |
Y![]() |
C ![]() |
68 ![]() |
Currently I have a VBA code that loops over each element multiple times to calculate difference between current and new averages and apply this difference to elements.目前我有一个 VBA 代码,它在每个元素上循环多次以计算当前平均值和新平均值之间的差异并将这种差异应用于元素。 Is there any matrix algorithm I can use that would make the same calculations instead of using iterative loops ?
我可以使用任何矩阵算法来进行相同的计算而不是使用迭代循环吗? Algorithm code will be implemented in Matlab, C++ or Python or VBA.
算法代码将在 Matlab、C++ 或 Python 或 VBA 中实现。 Currently I have been exploring MATLAB function
lsqlin
but cannot get it to do what I want.目前我一直在探索 MATLAB 函数
lsqlin
但无法让它做我想做的事。
Could you please help?能否请你帮忙?
Step1 : line 1-4 |步骤 1:第 1-4 行 | Step2 : line 5-8
步骤 2:第 5-8 行
Defining :定义:
Value & relation![]() |
"Current" case ![]() |
"new" case ![]() |
---|---|---|
a = sum(mth1,mth2,mth3) ![]() |
60 ![]() |
? ![]() |
b = sum(mth4,mth5,mth6) ![]() |
99 ![]() |
? ![]() |
c = sum(mth7,mth8,mth9) ![]() |
141 ![]() |
? ![]() |
d = sum(mth10,mth11,mth12) ![]() |
108 ![]() |
? ![]() |
avg_A = a/3 ![]() |
20 ![]() |
25 ![]() |
avg_Q = b/3 ![]() |
33 ![]() |
35 ![]() |
avg_S = (b+c)/6 ![]() |
40 ![]() |
32 ![]() |
avg_Y = (a+b+c+d)/12 ![]() |
34 ![]() |
39 ![]() |
In "Current" case : all variable above have defined values.在“当前”情况下:上面的所有变量都有定义的值。
In "new" case (Step3) :在“新”情况下(步骤 3):
avg_A = a/3 = 25 >> a = 25*3 = 75
avg_Q = b/3 = 35 >> b = 35*3 = 105
avg_S = (b+c)/6 = ((105)+c)/6 = 32 >> c = 32*6-105 = 87
avg_Y = (a+b+c+d)/12 = ((75)+(105)+(87)+d)/12 = 39 >> d = 201
Fill in mth1-12 with any value that can produce a,b,c,d value above.将 mth1-12 填入任何可以产生上述 a,b,c,d 值的值。
Done.完毕。
p/s: since OP only share 1 new "case", Step2 detail may varies. p/s:由于 OP 仅共享 1 个新“案例”,因此步骤 2 的详细信息可能会有所不同。 However, at least for this "shapes", this works.
然而,至少对于这个“形状”,这是有效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.