Thank you for looking and answering this question. I am trying to create a new matrix from an old one. Meaning, I have a original matrix that's 20x20. I want to keep the first five lines of the matrix, then from line 6-10 I want to take every other line, meaning line 7 and 9 would be deleted, but also column 7 and 9 need to be deleted. Then from line 11-20, I want to take every 3 lines, so the rows and columns need to be deleted accordingly.
Asked
Active
Viewed 647 times
3 Answers
7
I'll use this example matrix:
matrix = Table[i + j, {i, 20}, {j, 0, 19}]
Please take a look at it. You'll notice that the top row has the elements 1-20 and that the leftmost column as has those as well. Now let's create a list of the columns/rows that you want to keep:
keep = Join[Range[5], Range[6, 10, 2], Range[11, 20, 3]]
(* Out: {1, 2, 3, 4, 5, 6, 8, 10, 11, 14, 17, 20} *)
Then:
matrix[[keep, keep]]
This results in the matrix below (visualized with MatrixForm):

C. E.
- 70,533
- 6
- 140
- 264
-
-
-
1@seismatica Have a look at this: (20228) -- Pickett: +1 on a clean answer. :-) – Mr.Wizard Aug 08 '14 at 06:01
-
-
thanks for the ideas, the problem is that the matrix is going to be growing, and I need a systematic way to only take every 10th row, therefore I will have to delete a lot more rows and columns then in this 20x20 example. – user2558894 Aug 09 '14 at 06:47
-
1
producing some random data
data = RandomInteger[100, {20, 20}]
If I understood well, you mean
data[[{1, 2, 3, 4, 5, 6, 8, 10, 11, 14, 17, 20}]] // Transpose //
Part[#, Range@20~Complement~{7, 9}] & // Transpose
hieron
- 1,167
- 6
- 13
1
In other cases (few deletions) one could also consider Delete:
m = Partition[Range[400], 20];
del = {{7}, {9}, {12}, {13}, {15}, {16}, {18}, {19}};
m // Delete[del] // Transpose // Delete[del] // Transpose // MatrixForm

eldo
- 67,911
- 5
- 60
- 168