Get Parts of a Matrix
WORKFLOW
Get Parts of a Matrix
Pick out and extract parts of matrices.
Create a matrix
Use Table to set up a 5×5 matrix:
mat = Table[Subscript[m, i, j], {i, 5}, {j, 5}]Use MatrixForm to format the output as a regular array:
MatrixForm[mat]- Matrices in the Wolfram Language are not restricted to number entries.
Extract a single part
Use Part to pick the second element of the first row:
Part[mat, 1, 2]- Part has the short form notation
.
Extract a row
To take an entire row, use one index to specify the row:
mat[[4]]Extract a column
To take an entire column, select all the rows and specify the column:
mat[[All, 4]]Extract a submatrix
Use Span to specify the relevant rows and columns that make up a submatrix:
mat[[1 ;; 2, 1 ;; 3]]//MatrixFormExtract all elements except the outermost rows and columns using negative indices:
mat[[2 ;; -2, 2 ;; -2]]//MatrixForm- Negative indices count from the end of a list.