Section 2.7 Deleting Elements from Arrays
Deleting elements from arrays is accomplished by assigning nothing to the elements - the empty array
[]
.>> ve = 0:10
ve =
0 1 2 3 4 5 6 7 8 9 10
>> ve(6:8) = []
ve =
0 1 2 3 4 8 9 10
Similarly, elements can be deleted from matrices. Since we need to retain the rectangular shape of a matrix, we need to delete an entire column or an entire row at once. Take a look at this example:
>> ma = [0 1 2 3; 4 5 6 7; 8 9 10 11; 12 13 14 15]
ma =
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
>> ma(:, [2,4]) = []
ma =
0 2
4 6
8 10
12 14
In this example, we deleted columns 2 and 4 from the matrix, leaving us with columns 1 and 3 only.