LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Mathscript problems: Input array??

I have done a very simple mathscript problems, but still cannot get a desired result?
is there something wrong in my program??Please help....
 
 
0 Kudos
Message 1 of 8
(9,549 Views)
What would be the desired result? 😉
0 Kudos
Message 2 of 8
(9,542 Views)

I think i should get a array Y with 3 elements { -1,1,-1}

 

I attached my program, can anyone help me, give me some ideas? is there any wrong in the commands?

0 Kudos
Message 3 of 8
(9,538 Views)
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
Message 4 of 8
(9,531 Views)
So, do you mean that Mathscript cannot handle an array????It can only handle Scalar ??
0 Kudos
Message 5 of 8
(9,522 Views)

In fact, my original program is similiar to the attachment. When you play with it, you will find out what's wrong...

I wonder it is the error in LV to handle Mathscript command...

0 Kudos
Message 6 of 8
(9,523 Views)
Hello,

LabVIEW MathScript can handle arrays.  In fact, it handles arrays much more efficiently than it can handle scalars.  However, your algorithm is not constructed properly to perform the action you want.  It looks like you want to compute the square root of elements in your array.  If the element of the array is negative, then you want to negate it so you perform the square root of a positive number.  If so, this is a first pass at the code that will do what you want:
for i = 1:length(x)
    if x(i) < 0
        y(i) = sqrt(-x(i))
    else
        y(i) = sqrt(x(i))
    end
end


You need a loop to test each element individually.  However, this code will execute very slowly.  So, instead, let's look at the code you wrote.  The if test as you have written is equivalent to
if all(x < 0)
This means that a truth test is performed for all elements in the array, not element by element.  A more efficient way to perform your calculation is to see which elements are less than zero and perform a square root of the negated value of only those values.  We can perform this test on the vector as a whole rather than element by element.

y = zeros(size(x));     % Preallocate y (optional)
i = x < 0;              % Compute indices of x where value is negative
j = x >= 0;             % Compute indices of x where value is non-negative
y(i) = sqrt(-x(i));     % Compute square root of negated indices for negative values
y(j) = sqrt(x(j));      % Compute square root of non-negative values


The last two lines don't operate on the entire vector.  The second to last line only operates on values of x that are less than zero.  It only modifies the values in the same position in the y vector.  Similarly, the last line only operates on non-negative values of x.  Again, it only modifies the values in the same position in the y vector.  The result is that negative values have a square root computed on their negated values and non-negative values have a regular square root performed.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
Message 7 of 8
(9,513 Views)
After I thought about this some more, there is an even simpler method.  After looking at your code again, it was always performing a square root of a positive number.  But, if the original number was negative, it would set the result to be negative (the code I suggested does not do this last step).  To accomplish this as compactly as possible with LabVIEW MathScript, use the following line of code:

y = sqrt(abs(x)) .* sign(x);

Taking the absolute value removes the problem with negative numbers and the sign function will preserve the original sign of the number.

Grant M.
Staff Software Engineer | LabVIEW Math & Signal Processing | National Instruments
0 Kudos
Message 8 of 8
(9,465 Views)