Learning objectives:
- …
- …
- …
According to the inflammation study, there is something suspicious in our inflammation data by looking at the plots. In this section you will learn how you can use Julia to automatically recognize the different features we saw, and take a different action for each, i.e. you will learn how to write code that runs only when certain conditions are true.
The generic syntax for conditionals in Julia is:
if condition1
option 1
elseif condition2
option 2
else
option 3
end
For example, let’s check if a number is greater than 100. If yes, print “greater”, otherwise print “not greater”.
number = 37
if number > 100
println("greater")
else
println("not greater")
end
print("Done")
not greater
Done
Note that only the if part of the conditional is compulsory. The elseif and else parts are optional.
number = 37
if number > 100
println("greater")
end
print("Done")
You can add additional conditions using the elseif part (you can add as many you want). For example, let’s check if a number is positive, negative or equal to zero:
number = -3
if number > 0
println(number, " is positive")
elseif num == 0
println(number, " is zero")
else
println(number, " is negative")
end
-3 is negative
You can also combine conditions using the and (&) symbol, indicating that the condition is only true if both parts are true:
number = 53
if (number > 0) & (number < 100)
println("both parts are true")
else
println("at least one part is false")
end
both parts are true
In this example, we print “both parts are true” only if the number is greater than 0 and at the same time number is less than 100.
You can also combine conditions using the or (|) symbol (Shift+\ buttons in the keyboard), if you would like one or the other condition to be true
number = 53
if (number < 0) | (number > 100)
println("at least one part is true")
else
println("at least one part is false")
end
at least one part is true
In this case, if one of the two conditions is true, then we will print “at least one part is true”.
Exercise
You have the following code
if 4>5 println("A") elseif 4 == 5 println("B") elseif 4<5 println("C") end
Which of the following would be printed if you were to run this code? Why did you pick this answer?
- A
- B
- C
- B and C
In Julia, we can write conditionals in a more compact way, such as
a?b:c
which equates to
if a
b
else
c
end
For example, we have the following conditional
x=3
y=5
if x>y
println(x)
else
println(y)
end
which gives an output
5
We could write the previous conditional using Julia’s compact way
(x>y) ? println(x) : println(y)
which gives the exact same output
5
In this compact format, if we replace & with &&, as in a && b, we get short-circuit evaluation, i.e. b is only evaluated if a is true, which can help us out if evaluating b is expensive (time consuming). For example
(x > y) && println(x)
false
(x < y) && println(y)
5
Similarly, we can do the same as before with the OR operator, i.e. you can use the single OR operator if you would like to evaluate the whole conditionals
true | (println("hi"); true)
hi
true
or you can use the double OR operator || for short-circuit evaluation
true || (println("hi"); true)
true
Exercise
text text text
Let’s go back to the inflammation dataset to apply the conditional to find out if our dataset is OK for analysis or if there are any problems. We know that there is something suspicious in the inflammation data:
To check if each of the inflammation datasets has any of these two problems, we can use conditionals. Let’s start building our conditional step by step. Let’s start with the first rule about the maximum inflammation:
data = readdlm("./data/inflammation-01.csv", ',');
if (maximum(data,1)'[1]==0) & (maximum(data,1)'[21]==20)
println("Problem detected: Suspicious looking maximum!")
end
Now, let’s add the second rule using the elseif command:
data = readdlm("./data/inflammation-01.csv", ',');
if (maximum(data,1)'[1]==0) & (maximum(data,1)'[21]==20)
println("Problem detected: Suspicious looking maximum!")
elseif sum(minimum(data,1)')==0
println("Problem detected: Minimum add up to zero!")
end
and at the end let’s add a statement if the dataset is OK:
data = readdlm("./data/inflammation-01.csv", ',');
if (maximum(data,1)'[1]==0) & (maximum(data,1)'[21]==20)
println("Problem detected: Suspicious looking maximum!")
elseif sum(minimum(data,1)')==0
println("Problem detected: Minimum add up to zero!")
else
println("The dataset is OK")
end
So, if we run this conditional for the inflammation-01.csv dataset, the output will be
Problem detected: Suspicious looking maximum!
which means that we have detected a problem in the first dataset. Try to run this conditional on the first three dataset to check if the other two datasets also have a problem.