Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to form submatrices with some non-consecutive rows and columns of a matrix

I have a 10 by 10 matrix. I want to form a sub-matrix from this main matrix, using all the rows and columns except the 1st, 2nd and 8th columns and rows.
I know Part can be used to form the sub-matrix, but the examples are mostly about forming the sub-matrix using consecutive rows and columns only.

like image 800
Sreya Avatar asked Jun 06 '11 21:06

Sreya


2 Answers

If this is your matrix:

tst = RandomInteger[10, {10, 10}];

This will do the trick for the case at hand:

tst[[{3, 4, 5, 6, 7, 9, 10}, {3, 4, 5, 6, 7, 9, 10}]]

Instead of explicit list, you could use Complement[Range[10],{1,2,8}].

like image 70
Leonid Shifrin Avatar answered Nov 15 '22 13:11

Leonid Shifrin


Here's another way.

Call your matrix

test = Array[m, {10, 10}]

Then your sub matrix is

subTest = Nest[Delete[Transpose[#], {{1}, {2}, {8}}] &, test, 2]

Compare with Leonid's method

subTest == test[[#, #]] &[Complement[Range[10], {1, 2, 8}]]
(* True *)
like image 25
Simon Avatar answered Nov 15 '22 12:11

Simon