0

I am reading data from a file, and I determine the min and the max.

I would like to implement a function where if min==max, then min = 0

I have thios so far:

reset

# Define two helper functions
ismin(x) = (x<min)?min=x:0
ismax(x) = (x>max)?max=x:0

# Initialise the 'global' vars
max=-1e38
min=1e38

plot "Data.txt" u 0:(ismin($3)*ismax($3))

#set terminal windows
set terminal png medium size 900,600
set output "Data.png"

print min
print max

iseq(min,max) = (min == max)?min = 0 :0

print min
print max

How can I set min to 0 if min==max?

Thank you

2 Answers 2

1

How about an if statement:

if (min == max) min = 0
Sign up to request clarification or add additional context in comments.

Comments

0

It depends on what you want to do... since gnuplot 4.6.0 (March 2012) you have STATS (check help stats). Assuming you have the following data in SO15004547.dat:

Test A:

-99
  1
 55
 20

or alternatively Test B:

 33
 33
 33
 33

So, I see at least two ways:

Script:

### find min, max and set min=0 if min==max
reset session

FILE = "SO15004547.dat"

# version 1 using STATS
stats FILE u 1 nooutput
max = STATS_max
min = STATS_min == STATS_max ? 0 : STATS_min
print min, max

# version 2 using plot
min = max = NaN
getmin(x) = x>min ? min : x
getmax(x) = x<max ? max : x
plot FILE u 0:(min=getmin($1),max=getmax($1), $1)
min = min==max ? 0 : min
print min, max

### end of script

Result:

Test A:

-99.0, 55.0
-99.0, 55.0

Test B:

0, 33.0
0, 33.0

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.