LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculate with 1 D Arrays

Hi, everyone ok? I´m not really....

I´ve got 1D array inputs (x and y) in mathscript, sth like this:

 

if x>0
w=sqrt(x.^2+y.^2)  (the point is for element-wise calculation, i think)
elseif x<0
w=x+999
end

 

on the front panel i don´t get any numbers... maybe for arrays there is to heed sth. special....(i´ve already changed data type to correct value )

 

It´s my first time with mathscript.... if anyone also knows some good tutorials i would be grateful...

0 Kudos
Message 1 of 8
(7,793 Views)

Lecojon,

 

The "if x>0" will only return a true if ALL elements are greater than 0.  Likewise, the elseif x<0 will only return true if ALL elements are less than zero.  Chances are that your input array has both positive and negative values.  The rest of this assumes that you want your logic to operate element-wise, meaning if the first element of x is positive, then you want the first element of w to be the sqrt of sum of square of first elements in x and y, and if the first element of x is positive, then you just want to add 999 to it.  If that's the case, then you need to iterate through your input array before your if logic.

 

for ii=1:length(x)
     if x(ii)>0
          w(ii)=sqrt(x(ii)^2+y(ii)^2);
     elseif x(ii)<0
          w(ii)=x(ii)+999;
     end
end

 

 You'll also want to make either your> or < to be inclusive, meaning either (>= coupled with a <) or a (> coupled with a <=) in case one of your values is exactly 0.

 

With that, I get the following results:

ArrayMath.png

 

Regards,

 

0 Kudos
Message 2 of 8
(7,789 Views)
Hello Lecojon,

A lot of power of MathScript lies in logical indexing.  The same code that Jeffrey P posted can be vectorized using logical indexing.  This will result in greater run-time performance.  To create logical indices, simply perform a boolean test such as x > 0.  The resulting array of true/false values can then be used to index or replace another vector or matrix.  With a true/false array used in indexing, a true means to use the corresponding index at that value while a false means to ignore the corresponding index at that value.

b = x > 0;            % Create logical indices
w = zeros(size(x));   % Initialize the array
w(b) = sqrt(x(b).^2 + y(b).^2);   % Use indices of x > 0
w(~b) = x(~b) + 999;              % Use indices of x <= 0


Grant M.
Senior Software Engineer | LabVIEW MathScript | National Instruments
0 Kudos
Message 3 of 8
(7,785 Views)

Well, I´ve got more different input-arrays of the same size.

As i see in your example i can take the iteration index "i" for each array, so i don´t have to take f.ex. "j" for array 2 or "k" for array 3...

I´ve got to put a ";" after each calculation of mine....and "%" is for writing comments....

 

everything right?

 

In google there is no god tutorial, so i´m working with LV help and by guessing ^^....

0 Kudos
Message 4 of 8
(7,779 Views)

Sorry, forgot sth.

 

whats the syntax for two conditions?

 

if i wanna say:  x<0 and y>0 at the same time:

 

is that

 

if x<0 and y>0

.

.

.

0 Kudos
Message 5 of 8
(7,775 Views)

Hello lecojon,

 

For conjunction of conditions,

if <condition1> & <condition2>

<program>

end

Andy Chang
National Instruments
LabVIEW Control Design and Simulation
0 Kudos
Message 6 of 8
(7,711 Views)
i used "&&" and it worked, don´t know whether the program did that correct with that, but there was no error message...
0 Kudos
Message 7 of 8
(7,707 Views)
Hello,

The semicolon is optional at the end of each statement.  What it means is to suppress any output.  Otherwise, a command such as
b = x > 0
will display the value of b after it executes.

You are correct: the % is for writing comments.

When testing for two conditions, you have a choice between the & and the && operators.  The main difference is that the && operator is a short-circuit operator.  Short-circuit evaluation allows the logical operator to skip evaluating the second argument if it can determine the result of the comparison after evaluating the first argument.  Here is the truth table for the AND logical operator:

   || T | F |
 ============
 T || T | F |
 ------------
 F || F | F |


From this table we can see that if the first argument evaluates to false, the result will be false regardless of the value of the second argument.  Thus, the && operator will skip evaluating the second argument.  If your arguments are the output of function calls, this can save quite a bit of time.  However, also note that if your second argument has any side effects from its evaluation, they will not happen if the first argument is false.

Another difference is that the && operator will only work on scalars.  If you are testing arrays, you will need to use the & operator:
if x<0 & y>0

Note then that if x is an array, the result of the x<0 test is an array of boolean values (each element of x has been compared with 0).  In order for this argument to evaluate to true, ALL elements of the array must be true.  In other words, that was just a shorthand way to write
if all(x>0) & all(y>0)

Grant M.
Senior Software Engineer | LabVIEW MathScript | National Instruments
0 Kudos
Message 8 of 8
(7,690 Views)