Suppose that I want to write a function fun
that takes an Integer
num
as input, and returns Red
if num == 1
, Orange
if num == 2
, and Yellow
if num == 3
. One way, I think, to do this is to use Which
:
fun[num_Integer] := Which[num == 1, Red, num == 2, Orange, num == 3, Yellow]
Is there a more concise way to write fun
? In other words, is there any built-in function (analogous to Which
) that I can use that would allow me to avoid typing "num
" all the time instead of always having to specify it to Which
. Writing fun
as a series of nested If
statements would be even lengthier.
Do you have any suggestions? Thanks for your time.
Switch
as per @jVincent below $\endgroup$PiecewiseColor
. $\endgroup$