user129186
user129186

Reputation: 1238

Gnuplot, take the average of each 2 rows

I am trying to plot the average of every two rows. So gnuplot should take the first two rows, add them and then divide the result by two. Repeat the same thing on rows 3 and 4 etc..

So output of

10

20

30

40

would be a graph with points at

15

35

Any suggestions would be very welcome, thanks!

Upvotes: 1

Views: 505

Answers (1)

Karl
Karl

Reputation: 2187

plot n=0, dataf using 0:(n==0 ? (a=$1, n=1, NaN) : (n=0, ($1+a)/2))

The flag variable "n" changes from 0 to 1 at each point, if it is zero, the present value is saved to "a" and NaN is returned, if it is one, the average of the present value and a is returned.

Check "help ternary operator".

A comma in a mathematical expression means "do this consecutively and only return the result of the last part", thats called "serial evaluation".

The first part in the plot (n=0) is not actually plotted. It just makes sure n is always initialized correctly for replotting, e.g. when you zoom in your plot.

Upvotes: 1

Related Questions