Table Of Contents

Guidelines for Accessing MathScript Array Elements

Version:
    Last Modified: December 23, 2016

    Use specific syntax to access MathScript array elements.

    Guidelines Examples
    MathScript accesses array elements in the following order: rows, columns, and higher dimensions. Indexing arguments are 1-based.
    A = [1, 2; 3, 4]
    A (2,2) = 5

    Creates an array with two rows and two columns and assigns the value 5 to the array element in the second row and the second column.

    A = ones(2,2,2)
    A (1,1,2) = 5

    Creates a 3-dimensional array of ones and assigns the value 5 to the array element in the first row, the first column, and the second dimension.

    Use empty brackets to delete elements of arrays.
    A = [1, 2, 3, 4]
    A(3) = []

    Creates an array with four values and deletes the third element of the array.

    Use colons to address every element of an array dimension.
    A = [1, 2; 3, 4]
    A(1,:) = 7

    Creates an array with two rows and two columns and assigns the value 7 to every column of the first row.

    Create an array and assign values to it simultaneously.
    A(2,2) = 9

    Creates an array with two rows and two columns. The array element in the second row and the second column has the value 9. The other array elements have the value 0.

    Use Boolean expressions to write or read elements of an array.
    A = [1, 2, 3, 4]
    B = A(A>2)

    Creates an array with four values and assigns all values that are greater than 2 to the new array B.

    A = [1, 2, 3, 4]
    A(A>2) = 7

    Creates an array with four values and assigns the value 7 to all elements of the array that are greater than 2.

    Use end to specify the last array element of a specific dimension.
    A = [1, 2, 3, 4, 5, 6]
    A(3:end) = 7

    Creates an array with six values and assigns the value 7 to all elements, beginning with the third element.

    A = ones(3,3)
    A(5:end) = 2

    Creates a 2-dimensional array of ones and assigns the value 2 to all array elements, beginning with the fifth element.


    Recently Viewed Topics