01-21-2009 03:46 PM
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.
01-21-2009 07:28 PM
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.
01-21-2009 07:30 PM