Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to delete a matrix column in Mathematica

I am trying to delete both a matrix in mathematica. An inelegant way of doing it is as I do below, i.e specifying it in a new matrix as

S = Table[
    Ss[[If[i < t, i, i + 1]]][[If[j < t, j, j + 1]]], {i, q}, {j, q}];  

where the goal is to eliminate row and column t.

Indeed delete a line is easy Delete[Ss,t]. For the column column I suppose I could do

Transpose[Delete[Transpose[Ss,t]]]  

My primary concern is to do it in a way that executes the fastest way possible.

More generally, is there a Mathematica operator that makes it as easy to slice and dice matrix columns as it is to do for rows without resorting to transpose?

like image 251
Phil Avatar asked Mar 13 '11 12:03

Phil


People also ask

How do I remove a column from a matrix in Mathematica?

Drop can easily drop columns besides the first, e.g. Drop[m, 0, {3}] , and it is very fast. Part is also usually very fast, and allows assignments which is both flexible and efficient (when applicable). SlotSequence is simply fun and can be quite useful when you also want to do something with the elements.

How do you delete a row from a matrix in Mathematica?

Null, M = Delete[Transpose[M], b], M = Delete[Transpose[M], a]]; Transpose[M] ]; To remove a columns and b rows. This works for a square matrix provided I am removing the same rows/columns.


1 Answers

I think you are looking for:

Drop[Ss,{t},{t}]  

Timings:

ClearAll["Global`*"];

First@Timing[a = RandomInteger[1000, {5000, 5000}];]
0.34

First@Timing[Drop[a, {2}, {2}]]
0.11

While

First@Timing[Transpose@Delete[Transpose@Delete[a, 2], 2]]
0.5
like image 184
Dr. belisarius Avatar answered Sep 21 '22 07:09

Dr. belisarius