Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

awk - bash: calculation of the RMSD from the dataset

I have a my_file.xvg contained 240 lines with the numbers arranged in the following format:

    5.4
    5.1
    5.2
    5.4
    5.4
    4.9
    5.0
    5.2
....
    4.9

Using awk I have already calculated the mean value of these data and store it as a "mean" variable in the bash script:

mean=$(awk '{sum+=$1}END{printf "%.1f", sum/NR}' my_file.xvg)

How could I calculate RMSD of these numbers (to determine error of the mean for instance) and store it as the another variable?

question from:https://stackoverflow.com/questions/66047971/bash-calculation-of-the-rmsd-from-the-dataset

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Once the value of the mean is saved in the variable a very similar approach can be used for the RMSD. As you seem to prefer awk, see the following:

rmsd=$(awk -v mean=$mean '{++n;sum+=($NF-mean)^2} END{if(n) print sqrt(sum/n)}' my_file.xvg)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...