Using the C++ Math Library | ![]() ![]() |
Deleting Elements from an Array
You can use indexing expressions to delete elements from an array. Deletion is a special case of using indexing expressions in assignment statements. Instead of assigning a new value to an element in an array, you assign the null array to a position in the array. The MATLAB C++ Math Library interprets that assignment as a deletion of the element and shrinks the array.
For example, to delete an element from example matrix A
, you assign the null array to that element. You create a null array with the empty()
function.
When you delete a single element from a matrix, the matrix is converted into a row vector that contains one fewer element than the original matrix. For example, when element (8
) is deleted from matrix A
A(8) = empty();
matrix A
becomes this row vector with element 8
missing:
1 2 3 4 5 6 7 9
You can also delete more than one element from a matrix, shrinking the matrix by that number of elements. To retain the rectangularity of the matrix, however, you must delete one or more entire rows or columns. For example,
A(2,colon()) = empty();
produces this rectangular result:
1 4 7 3 6 9
Note that the right side of an assignment statement that expresses a deletion is always a call to empty()
. The left side of the assignment statement must be a valid indexing expression. The null array is applied to each element selected by the subscript.
Similar to reference and assignment, two-dimensional deletion extends to N dimensions. If A
has more than two dimensions, simply specify more than two dimensions as indices in the subscript.
![]() | Extending Two-Dimensional Assignment to N Dimensions | Indexing into Cell Arrays | ![]() |