#! /bin/bash
#
# cor - Read pairs from std-in, one per line, and write to std-out the
# correlation of the points.
gawk '{ if (NF != 2) {
print "Input should have 2 fields, not " NF "." > "/dev/stderr"
exit 1
}
sum_x += $1
sum_y += $2
sum_xy += $1*$2
sum_xsq += $1*$1
sum_ysq += $2*$2
}
END {
if (NR > 0) {
n = NR*sum_xy - sum_x*sum_y
d = (NR*sum_xsq - sum_x*sum_x)*(NR*sum_ysq - sum_y*sum_y)
print n/sqrt(d)
}
else
print 0
}
'
# $Log: cor,v $
# Revision 1.1 2005/04/04 03:07:29 rclayton
# Created.
#
syntax highlighted by Code2HTML, v. 0.9