how to take second column of a matrix in matlab

In MATLAB, you can take the second column of a matrix by using the colon operator : to select all rows of the matrix and the index 2 to select the second column.

main.m
matrix = [1 2 3; 4 5 6; 7 8 9];
second_column = matrix(:, 2);
62 chars
3 lines

The output of second_column will be:

main.m
2
5
8
6 chars
4 lines

In the code above, the : operator specifies that we want to select all rows of the matrix, and the 2 specifies that we want to select the second column. The resulting vector second_column is a column vector containing only the elements of the second column of matrix.

related categories

gistlibby LogSnag