CHOOSE Function

CHOOSE( condition, result-for-true, result-for-false ) ;

The CHOOSE function examines each element of the first argument for being true (nonzero and not missing) or false (zero or missing). For each true element, it returns the corresponding element in the second argument. For each false element, it returns the corresponding element in the third argument.

The arguments to the CHOOSE function are as follows:

condition

is checked for being true or false for each element.

result-for-true

is returned when condition is true.

result-for-false

is returned when condition is false.

Each argument must be conformable with the others (or be a scalar value).

For example, suppose that you want to choose between and according to whether is odd or even, respectively. You can use the following statements to execute this task, as shown in Figure 23.59:

x = {1, 2, 3, 4, 5};
y = {101, 205, 133, 806, 500};
r = choose(mod(x#y,2)=1, x, y);
print x y r;

Figure 23.59 Result of the CHOOSE Function
x y r
1 101 1
2 205 205
3 133 3
4 806 806
5 500 500

As another example, the following statements replace all missing values in the matrix z with zeros, as shown in Figure 23.60:

 z = {1 2 ., 100 . -90, . 5 8};
 newZ = choose(z=., 0, z);
 print z, newZ;

Figure 23.60 Replacement of Missing Values
z
1 2 .
100 . -90
. 5 8

newZ
1 2 0
100 0 -90
0 5 8