ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

LabVIEW MathScript RT Module

cancel
Showing results for 
Search instead for 
Did you mean: 

Square Values in Array?

I have an 1D array of type double named "E."  From each entry in "E," I want to subtract the same value, "Eg."  I then want to square each of the values in the resulting array, i.e. first value in final desired array is [E(1)-Eg]^2, second value is [E(2)-Eg]^2, etc.

 

It seems that MathScript will not allow me to square an array?  When I use the code

 

EEg=E-Eg

EEgSquared=EEg^2

 

I am able to obtain my array EEg just fine.  I only get an array full of zeros when I attempt to perform the squaring operation, however.

 

Suggestions?  Thanks.
0 Kudos
Message 1 of 3
(7,203 Views)

Hi,

 

You should use operator '.^' for element-wise square. See the following script.

a = rand(10, 1);
a = a - 1;
a = a.^2;

The '^' operator is used to perform square of matrix, i.e., A*A. Therefore, it is necessary that the matrix is square. Note that scalar in MathScript is also treated as a 1-by-1 matrix. However, a vector is not a square matrix.

 

In MathScript, you can always use '.' to specify that the operator is applied element-wise for a matrix or a vector, such as '.*', './'. Otherwise, MathScript performs matrix operation, which is different with scalar operation for multiplication, division, square, etc.

0 Kudos
Message 2 of 3
(7,199 Views)
Ah, I knew there had to be a trick.  Thanks!
0 Kudos
Message 3 of 3
(7,196 Views)