
Place a semicolon after each line of script.
Details
When you place a semicolon after a line of script, MathScript does not display the output for that command. Not using semicolons causes unbounded execution times for the script.

Note You do not need to place semicolons after the first lines of for and while loops or switch and ifelse statements, or after else and end in these constructs. 


Write scripts and userdefined functions you use within timecritical VIs and timed structures with recommended syntax.
Details
National Instruments has tested a subset of syntax possible in MathScript, such as matrix operations and loops, and found that this syntax typically executes in a bounded amount of time when used according to the guidelines in this topic. Other syntax might cause increased jitter and unbounded execution times in the application.


Make sure inputs to operators are of the smallest possible data type. Also, make sure values are the smallest possible shape when you replace a portion of a vector or matrix with another value.
Details
For certain operations, LabVIEW might not return the smallest possible data type. For example, if you multiply a row vector and a column vector, LabVIEW might return a oneelement matrix that contains the result rather than a scalar number because the result of multiplying two vectors might be a matrix. Index the scalar result from the matrix as shown in the following example:
a = ones(1, 3);
b = ones(3, 1);
c = a * b;
d = c(1) + 1.5;
To display the data type of a variable, enable data type highlighting or click outside the MathScript Node and then move the cursor over a variable in the script to display the data type in the Context Help window.


Preallocate arrays that you define in a loop, and avoid resizing arrays inside loops.
Details
Do not change the size of arrays inside for and while loops. For example, avoid syntax that resizes arrays as a loop iterates, such as the following example:
a = [];
for i = 1:5
a = [a, i];
end
Instead of building an array in a loop, use the ones or zeros functions to preallocate the maximum number of elements you might need, and then replace the elements as shown in the following example:
a = zeros(1, 5);
for i = 1:5
a(i) = i;
end
If you do create an array inside a loop, do not use the array outside of the loop or during a subsequent iteration of the loop. For example, in the following script, temp is defined on the first iteration of the for loop and then used on the second and third iterations:
a = zeros(2, 5);
for i = 1:3
if i == 1
temp = [2; 2];
else
temp = 2 * temp;
a(:, i) = temp;
end
end
However, you can use syntax similar to the following example, where temp is defined and used on each iteration of the loop and the same array is not used in multiple iterations:
a = zeros(2, 5);
for i = 1:3
temp = [i; i + 1];
a(:, i) = temp;
end


Define matrices with scalar elements only. Do not use vectors or other matrices to define a matrix.
Details
For example, if you shift the order of columns in a matrix, do not redefine the matrix with the column vectors from the original matrix, as shown in the following example:
a = [1, 4, 7; 2, 5, 8; 3, 6, 9];
a = [a(:, 2:3), a(:, 1)];
Instead, you can index the columns from the original matrix to reshape the matrix, as shown in the following example:
a = [1, 4, 7; 2, 5, 8; 3, 6, 9];
temp = a(:, 1);
a(:, 1:2) = a(:, 2:3);
a(:, 3) = temp;


When you specify a start:stop range to index a vector or matrix, explicitly define the range rather than defining the range in a variable.
Details
For example, do not use the following syntax to return the second through fourth elements of the vector:
a = [1, 4, 8, 3, 9, 12];
b = 2:4;
a(b);
Instead, use syntax such as a(2:4); or the following example to return the same range of three elements from the vector:
a = [1, 4, 8, 3, 9, 12];
b = 2;
a(b:b + 2);


Avoid logical indexing operations.
Details
For example, do not use the following syntax, which replaces all values less than 5 in the matrix with 1:
a = [1, 4, 8; 3, 9, 12];
a(a < 5) = 1;
Instead, use the following syntax to perform the same operation without logical indexing:
a = [1, 4, 8; 3, 9, 12];
[n, m] = size(a);
for(i = 1:(n * m))
if(a(i) < 5)
a(i) = 1;
end
end


Do not resize matrices during replaceindexing operations.
Details
Do not grow the size of a matrix by indexing beyond the size of the matrix. For example, avoid the following syntax, which adds a fourth column to the existing 3by3 matrix:
a = zeros(3, 3); a(:, 4) = 0;
Also, do not use the [] operator to contract a matrix. Avoid syntax such as the following, which removes the second row of the existing matrix:
b = [4, 2; 7, 3]; b(2, :) = [];
