How to extract a column from a matrix in Matlab and Octave
To extract a column of a matrix in Matlab and Octave, specify the column number (n) and colon (:) in place of the row number.
M(:,n)
This command extracts all the values of the nth column from the matrix.
Example
Define a matrix 2x3
X = [1 2 1 ; 3 1 1]
The matrix has two rows and three columns
1 2 1
3 1 1
To extract the first column, enter X(:,1)
X(:,1)
This command extracts the first column of the matrix
1
3
To extract the second column type X(:,2)
X(:,2)
This command extracts the second column of the matrix
2
1
To extract the third column type X(:,3)
X(:,3)
The command extracts the third column of the matrix
1
1