1
$\begingroup$

Consider a matrix

(mat = Array[a, {5, 5}]) // MatrixForm

enter image description here

If I want to extract columns 2 through 4 which would return a 5x3 matrix, I can do that with:

mat[[;; , 2 ;; 4]] // MatrixForm

enter image description here

But what if I want to extract only column 2 and 4 (without column 3), so that I get a 5x2 matrix? Is there a short syntax to do that?

$\endgroup$
1
4
$\begingroup$

Rows 2 and 4 can be extracted with mat[[{2, 4}]].

Columns 2 and 4 can be extracted with mat[[All, {2, 4}]] or equivalently mat[[;;, {2, 4}]], or even Part[mat, All, {2, 4}].

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.