Simple way to delete a matrix column in Mathematica

谁说我不能喝 提交于 2019-12-09 03:03:20

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/5289350/simple-way-to-delete-a-matrix-column-in-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!