0

I have a file with different blocks like the following:

1 2 6 7
9 3 4 5
.......
3 6 2 9


4 7 2 7
9 5 3 7
.......
8 3 5 1

If i run stat 'File' the different blocks are recognized. Now I would like to do an average among all the blocks for each element of the blocks (i.e. in the example the resulting element of the 1st row and 1st column will be avg(1, 4)=2.5).

Is there any way to do this in gnuplot?

AMasc
  • 1
  • 3

2 Answers2

0

You can use external tools to calculate the averages of all cells over the blocks. But you can also do it in gnuplot. However, you need to have your data in a datablock. How to get it there, see: gnuplot: load datafile 1:1 into datablock. Assumption is that there are no headers and each subblock has equal number of rows. Check the following example as a starting point.

Code:

### calculate average over blocks for each cell in subblock
reset session

$Data <<EOD
111   112   113   114   115
121   122   123   124   125
131   132   133   134   135
141   142   143   144   145


211   212   213   214   215
221   222   223   224   225
231   232   233   234   235
241   242   243   244   245


311   312   313   314   315
321   322   323   324   325
331   332   333   334   335
341   342   343   344   345
EOD

stats $Data u 0 nooutput
BlockCount = STATS_blocks
ColCount = STATS_columns
RowCount = STATS_records/STATS_blocks

Cell(b,r,c)  = real(word($Data[b*(RowCount+2) + r],c))
CellAvg(r,c) = (sum [_b=0:BlockCount-1] (Cell(_b,r,c)))/BlockCount

set print $Avg
    do for [r=1:RowCount] {
        Line = ''
        do for [c=1:ColCount] {
            Line = sprintf("%s %g",Line,CellAvg(r,c))
        }
        print Line
    }
set print

set key noautotitle
set palette defined (0 "green", 1 "yellow")

plot $Avg matrix u 1:2:3 w image, \
       '' matrix u 1:2:(sprintf("%g",$3)) w labels
### end of code

Result:

enter image description here

theozh
  • 22,244
  • 5
  • 28
  • 72
0

Yes, there is.

data and mean

Here's the code to calculate the matrix of the cumulated value between blocks, and return the mean when reaching the last block.

$Data <<EOD
1 2 6 7
9 3 4 5
3 6 2 9


4 7 2 7
9 5 3 7
8 3 5 1
EOD

set xlabel "j"
set ylabel "i"
splot $Data matrix u 1:($2+0.1*column(-2)):3:-2 with impulses  lc pal z lw 2 title ""

N=3
M=4
B=2
array A[N*M]

do for [k=1:|A|] { A[k]=0 }

mymean(j,i,v,b)=(A[i*M+j+1]=A[i*M+j+1]+v,(b==B-1)?A[i*M+j+1]/B:1/0)

replot $Data matrix u 1:($2+0.05):(mymean($1,$2,$3,column(-2))) w p pt 2 ps 3

It makes use of column(-2), the block number. The values of M, N and B can of course be obtained by a first stat pass.

Joce
  • 2,220
  • 15
  • 26