找出由其他指标控制的参数的平均值 [英] Finding the average of parameters controlled by other indices
问题描述
在这个问题上我需要一些帮助
I need some help in this problem
我在MATLAB中有这个矩阵:
I have this matrix in MATLAB:
A = [ 25 1.2 1
28 1.2 2
17 2.6 1
18 2.6 2
23 1.2 1
29 1.2 2
19 15 1
22 15 2
24 2.6 1
26 2.6 2];
第一列是一些温度测量值
1st column is some measured values for temperature
第二列是代表颜色(1.2:红色,....等)的索引代码
2nd column is an index code representing the color (1.2:red,.....etc)
第三列是采样时间.仅在1到2小时内
3rd column is the hour of taking the sample. Only at hours from 1 to 2
我希望矩阵由第二列控制,如下所示:
I want the matrix to be controlled by 2nd column as follows:
如果为1.2,程序将在第1小时找到所有温度的平均值
if it is 1.2, the program will find the average of all temperatures at hour 1 that
对应于1.2
所以,这里(25 + 23)/2 = 24
So, here ( 25 + 23 )/2 = 24
并找到第2小时所有温度的平均值,并与之对应
and also finds the average of all temperatures at hour 2 and that corresponds
至1.2,(28 + 29)/2 = 28.5
to 1.2, ( 28 + 29 ) /2 = 28.5
和该平均值:
[24
28.5]
将替换第1和2小时的所有温度值
will replace all temperature values at hours 1 and 2
对应于1.2.
然后,它对索引2.6和15执行相同的操作
Then, it does the same thing for indices 2.6 and 15
因此,所需的输出将是:
So, the desired output will be:
B = [ 24
28.5
15.5
22
24
28.5
19
22
15.5
22]
我的问题是使用循环.我一次只能运行一个索引.
My problem is in using the loop. I could do it for only one index at one run.
例如,
T=[];
index=1.2;
for i=1:length(A)
if A(i,2)==index
T=[T A(i,1)];
else
T=[T 0];
end
end
所以,T是提取的T,它对应于1.2,其他条目为零
So, T is the extracted T that corresponds to 1.2 and other entries are zeros
然后,我写了很长的代码来找到平均值,最后我找到了矩阵
Then, I wrote long code to find the average and at the end I could find the matrix
仅与索引1.2相对应:
that corresponds to ONLY the index 1.2 :
B = [24
28.5
0
0
24
28.5
0
0
0
0]
但这仅用于一个索引,并且为其他索引分配零.我可以所有人做到这一点
But this is only for one index and it assigns zeros for the other indices. I can do this for all
在单独的运行中显示索引,然后添加B,但这距离我的真实情况要花费很长的时间
indices in separate runs and then add the B's but this will take very long time since my real
矩阵乘8是8760.
我敢肯定,这样做的方法更短.
I am sure that there is a shorter way to do that.
谢谢
致谢
推荐答案
尝试一下:
B = zeros(size(A, 1), 1);
C = unique(A(:, 2))';
T = [1 2];
for c = C,
for t = T,
I1 = find((A(:, 2) == c) & (A(:, 3) == t));
B(I1) = mean(A(I1, 1));
end
end
修改
我认为您对c = 2.6
和t = 1
的预期答案是错误的...不应该是(17 + 24)/2 = 20.5
吗?
Edit
I think your expected answer is wrong for c = 2.6
and t = 1
... Shouldn't it be (17 + 24)/2 = 20.5
?
这篇关于找出由其他指标控制的参数的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!