从计算矩阵元素明智的平均值 [英] computing element-wise averages from matrices
本文介绍了从计算矩阵元素明智的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一组存储在文本文件矩阵。我想计算所得的输入矩阵的逐元素的平均值的输出矩阵。下面的图例所示:
猫FILE1.TXT
Item0项目1
Item0 1.01456e + 06 5
项目1 2 12.2
猫FILE2.TXT
Item0项目1
Item0 1.0274e + 06 6
项目1 0 14.5
猫output.txt的
Item0项目1
Item0 1020980 5.5
项目1 1 13.35
请注意,某些输入矩阵的值都在工程符号
。
所有建议都欢迎!
解决方案
的awk -v行= 2:3 -v COL = 2:3 -v NUM = 2'开始 {
分裂(行,R,:)
拆分(COL,C:)
N = NUM
}ř[1] = FNR&放大器;&放大器; FNR&所述; = R [2] {
对(我= C [1]; I< = C [2];我++)
{
M [FNR,我] + = $ I
}
}结束 {
为(ⅰ= R [1]; I&下; = R [2];我+ +)
{
为(J = C [1]; J&下; = C [2]; J ++)
{
的printf(%F \\ t的,M [I,J] / N)
}
打印
}
}'文件{1,2} .TXT
1020980.000000 5.500000
1.000000 13.350000
I've a set of matrices stored in text files. I would like to compute an output matrix resulting of the element-wise averages of the input matrices. An illustration is given below:
cat file1.txt
Item0 Item1
Item0 1.01456e+06 5
Item1 2 12.2
cat file2.txt
Item0 Item1
Item0 1.0274e+06 6
Item1 0 14.5
cat output.txt
Item0 Item1
Item0 1020980 5.5
Item1 1 13.35
Note that some of the values in the input matrices are in engineering notation
.
All suggestions are welcomed!
解决方案
awk -v row=2:3 -v col=2:3 -v num=2 '
BEGIN {
split(row, r, ":")
split(col, c, ":")
n = num
}
r[1]<=FNR && FNR<=r[2] {
for(i=c[1];i<=c[2];i++)
{
m[FNR,i]+=$i
}
}
END {
for(i=r[1];i<=r[2];i++)
{
for(j=c[1];j<=c[2];j++)
{
printf("%f\t", m[i,j]/n)
}
print ""
}
}' file{1,2}.txt
1020980.000000 5.500000
1.000000 13.350000
这篇关于从计算矩阵元素明智的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文