Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...