Hello,
When you perform a relational operation on an array, LabVIEW MathScript computes the result elementwise. Thus, the result of
x < 0
is a boolean array: [1 0 1].
When you pass an array as the condition to an if statement, MathScript evaluates if the condition is true or false by performing a mathematical forall operation. The MathScript command for this is "all." Since all elements in the array are not true, the if evaluates the condition as false and executes the false case.
You say that your desired result is an array y of three elements. The text you wrote won't compute an array. You are only assigning a scalar to y. You could write a loop to process each element of the x array, but this would be very inefficient. If you want an array where the elements are +1 if the element of the other array was positive and -1 if the element of the other array was negative, the following code will do what you want:
y = x > 0;
y = y * 2;
y = y - 1;
Depending on how you want to treat 0, you can adjust the > operation to be >=. If you want the elements of y to be zero where x was zero, you can add one final line to the code above:
y(x == 0) = 0;
This last line will determine which indices of x are zero. It will then use those indices in y to set the value to zero.
Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments