How to remove a matrix column in Matlab and Octave
To delete a column of a matrix in Matlab / Octave, type
M(:,n)=[ ]
- M is the name of the array (two-dimensional array)
- n is the number of the column to be deleted
- [] is an empty vector
Examples
Example 1
Define a 2x4 matrix
>> M = [ 1 2 3 4 ; 5 6 7 8 ]
M =
1 2 3 4
5 6 7 8
To delete the first column of the matrix, type M(:,1)=[ ]
>> M(:,1)=[]
M =
2 3 4
6 7 8
The first column is removed from the matrix
The result is a 2x3 matrix
Example 2
Define a 3x3 matrix
>> M = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
M =
1 2 3
4 5 6
7 8 9
To remove the second column of the matrix type M(:,2)=[ ]
>> M(:,2)=[]
M =
1 3
4 6
7 9
This command deletes the second column of the matrix
The result is a 2x3 matrix.