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.
Asked
Active
Viewed 1,934 times
6

Sjoerd C. de Vries
- 16,122
- 3
- 42
- 94

Sreya
- 61
- 2
-
Sreya, welcome on StackOverflow! Don't forget to vote for the answer(s) below that you like and, if one of them answers your question to your satisfaction, please accept it by using the check-mark next to the answer. You can change your choice whenever you like. – Sjoerd C. de Vries Jun 06 '11 at 22:36
-
2See [here](http://stackoverflow.com/q/5299798/499167) for a closely related SO question which might be of interest. WReach has described two very useful functions, `takeOperator` and `dropOperater` which will also do what you ask, I think. See [here](http://stackoverflow.com/questions/5299798/efficient-way-to-pick-delete-a-list-of-rows-columns-in-a-matrix-in-mathematica/5300892#5300892) – 681234 Jun 07 '11 at 06:07
2 Answers
7
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}]
.

Leonid Shifrin
- 22,449
- 4
- 68
- 100
6
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 *)

Simon
- 14,631
- 4
- 41
- 101
-
Comparing the timings (for `100000` repetitions) of Leondid's method with mine shows that his is about 2.5 times faster.... – Simon Jun 06 '11 at 22:14
-
@belisarius: On that, when you transpose a matrix in Mathematica does it create a new object with the transposed elements or just flag the old one as transposed (like matlab or NumPy would)? – Simon Jun 06 '11 at 22:39
-
1@Simon A naive experiment seems to tell it duplicates the memory usage – Dr. belisarius Jun 06 '11 at 23:03
-
3
-
Copy & Paste :) ClearAll["Global`*"]; Print[x = MaxMemoryUsed[]]; r = Table[10. i, {i, 1000}, {j, 100}]; Print[y = MaxMemoryUsed[]]; r = Transpose@r; Print[z = MaxMemoryUsed[]]; Print[y - x, " ", z - y]; – Dr. belisarius Jun 06 '11 at 23:11
-
@belisarius: Not too convincing... `MaxMemoryUsed` includes the data stored in the `Out` variables.... – Simon Jun 07 '11 at 01:15
-
@Simon Add `$HistoryLength = 0;` and the result is the same (It's my default :) ) – Dr. belisarius Jun 07 '11 at 01:28
-
1@belisarius I am curious; do you find utility in `%`, `%%` etc., but forgo these to save memory, or do you have no use for them? – Mr.Wizard Jun 07 '11 at 01:33
-
@Mr. I find them deceiving. But that is perhaps because my programming "style" is incremental. I find myself refining the same expression several times, and recalculating outputs in different order. So I don't find much use for % et al, as the next time I need to use the expressions, they are something else. Of course I could use `%3143`, but I hate that. – Dr. belisarius Jun 07 '11 at 02:02
-
@belisarius I see your point, but I find having to name everything I use quite inconvenient. Most frequently I end up replacing `%` with `#` so recently I have taken to writing something like `Length@% / Max@%` instead as `Length@# / Max@# &@%` which results in less rewriting. – Mr.Wizard Jun 07 '11 at 05:09
-
@Mr. Perhaps you're right, but I tend to write each expression in a new cell while testing to have the expression and its output grouped together. And I don't like having to recalculate a previous cell (which may depends on % also!) to recalculate the next. Probably my problem comes from having learning Mma without much contact with other users, and so I lack some frontend tricks. – Dr. belisarius Jun 07 '11 at 05:18
-
@belisarius I did not mean to imply there is a problem with what you do. You could even argue it is superior because it works with `$HistoryLength=0`. I am trying to visualize your coding process. If you have the time to put together an illustration I am interested in seeing it. – Mr.Wizard Jun 07 '11 at 05:38
-
@belisarius I also like Simon's solution. It is strange that the double `Transpose` solution did not occur to me this time, given my answer in this (somewhat related) thread: http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/be4099e502cc12b4 – Leonid Shifrin Jun 07 '11 at 12:49
-
@Leonid Yep. Your solution there is using the same construct. I think your choice of taking `Part[ ]` is the most natural here, and the first it came to my mind too. – Dr. belisarius Jun 07 '11 at 13:19
-
@belisarius A pity I did not know about the solution by @WReach, or I would have linked it, or even refrain from answering and vote for a duplicate (which it is not exactly though). Which brings up the following concern: as the number of questions grow, it would be hard to remember all such duplicate or similar cases (for example, I think I am monitoring SO as close as anyone, but still missed this). I don't think the tagging system is effective enough. I wonder how other SO communities fight duplication (which IMO can be quite harmful) - is it enough to just mutually link similar questions? – Leonid Shifrin Jun 07 '11 at 13:31
-
@Leonid In this tag, the dups problem is not very big. There are really popular tags with traffic up to a hundred times this. Personally I don't think it is a critical thing. We are not trying to write book here, just helping people. You have a question, you get a few answers, or a pointer to a previous similar question. I feel many dups are a nuisance, but a few don't hurt that much. – Dr. belisarius Jun 07 '11 at 13:39
-
@belisarius I see. Perhaps I was thinking more about people who search for something (say with Google) and come to SO. They can of course always ask, but it would be nice if at some point there will be a better way for them to find the answer if it exists already, before asking. This can still take a big burden off those who aswer. But you must be right - for smaller communities like ours, this is not much of an issue, and for larger ones the pool of answerers is also much larger. – Leonid Shifrin Jun 07 '11 at 14:18