1

I would like to count the number of occurrences of an event (for example, x data value equals some number) and store these occurrences in order, while plotting a file in gnuplot. Say I have the following file:

1
0
0
0
1
1
0

Now I want to count how many times I have a 1 and store that number in variable N. Then I want to know the positions where that happens and store that information in an array pos, all of this while plotting the file. The result, for the example above, should be:

print N
3
print pos
1 5 6

I know how to achieve the counting:

N = 0
plot "data" u ($0):($1 == 1 ? (N = N+1, $1) : $1)
print N
3

Then to achieve the position recording, it would be schematically something like this:

N = 0 ; pos = ""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = pos." ".$0, $1) : $1) # This doesn't work!
print N
3
print pos
1 5 6

How can this be done in gnuplot without resorting to external bash commands?

1 Answer 1

1

Well, as sometimes happens writing down the question triggers an idea for an answer. I'll leave it here in case somebody finds it useful:

N=0 ; pos=""
plot "data" u ($0):($1 == 1 ? (N = N+1, pos = sprintf("%s %g", pos, $0+1), $1) : $1)
print N
3
print pos
1 5 6

Note I had to use $0+1 because position 1 is treated by gnuplot as zero.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.