使用Delete从方阵中删除行和列
给定:方阵,list表示要删除的行的索引,同时也表示要删除的列的索引(因为是方阵,所以只需要一个list)。
输出:方阵,删除列表中的行和列。
假设有效的索引列表。
这是一个例子
所以上面说要删除第二行和第四行,以及第二行和第四列。
我不知道如何使用 Delete[]
同时删除行和列,而且我真的不想为要删除的每个单独元素索引创建一个列表。
但我可以使用 Delete
来删除行。
下面是我解决这个问题的方法,我首先删除行,然后转置矩阵,然后删除新矩阵的行(这将是原始矩阵的列),然后将结果转置回去以获得我想要的结果。
像这样:
a = {{0, 5, 2, 3, 1, 0}, {4, 3, 2, 5, 1, 3}, {4, 1, 3, 5, 3, 2}, {4,
4, 1, 1, 1, 5}, {3, 4, 4, 5, 3, 3}, {5, 1, 4, 5, 2, 0}};
del = {{2}, {4}};
a = Delete[a, del];
a = Delete[Transpose[a], del];
(a = Transpose[a]) // MatrixForm
我的问题:是否有更短的方法使用删除(或另一种专家技巧)以更好的方式做到这一点?
谢谢
Given: square matrix, and list which represents the index of rows to be removed, and it also represent at the same time the index of the columns to be removed (it is square matrix, so only one list is needed).
output: the square matrix, with BOTH the rows and the columns columns in the list removed.
Assume valid list of indices.
This is an example
So the above says to remove the second and the 4th rows, and also the second and the 4th column.
I could not figure how to use Delete[]
to remove both rows and columns at the same time, and I really did not want to make a list of each individual element index I want to remove.
But I could use Delete
to remove rows.
This below how I solved it, I removed the rows first, then transposed the matrix, and then removed the rows of the new matrix (which will be the columns of the original), then transposed the result back to obtain what I wanted.
like this:
a = {{0, 5, 2, 3, 1, 0}, {4, 3, 2, 5, 1, 3}, {4, 1, 3, 5, 3, 2}, {4,
4, 1, 1, 1, 5}, {3, 4, 4, 5, 3, 3}, {5, 1, 4, 5, 2, 0}};
del = {{2}, {4}};
a = Delete[a, del];
a = Delete[Transpose[a], del];
(a = Transpose[a]) // MatrixForm
my question: Is there a shorter way using Delete (or another one of those expert tricks) to do this in a better way ?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想删除相同的索引列和行,我将使用
Part
。例如,要查看删除了第 2 行和第 4 行的a
:为了使其更通用,您可以创建一些将
DeleteCases
与Range
组合起来的内容> 和列/行索引列表,但在没有进一步信息的情况下,我还没有尝试这样做。编辑
编辑2
针对您的现实问题测试这两个内容。
In cases where you want to remove the same indexed columns and rows I would use
Part
. For example to seea
with columns and rows 2 and 4 removed:To make it more general you could create something in which you combine
DeleteCases
withRange
and the list of column/row indexes but in the absence of further information I haven't tried to do that (yet).Edit
Edit2
test both of these for your real world problem.
这应该是比双转置方法更快的删除行的方法:
时间各不相同,但在这个有些人为的示例中它更快:
{0.25,Null}
(0.125,Null}
This should be a faster way to delete rows than the double transpose method:
Timing varies, but in this somewhat contrived example it is faster:
{0.25, Null}
(0.125, Null}
这比
Part
方法效率低得多,但我发现它更透明,而且有时更重要。This is much less efficient than the
Part
method, but I find it somewhat more transparent, and there are times that matters more.您当然可以使用 Drop:
我不知道这有多普遍,但也许它会帮助您朝着正确的方向前进。
You can certainly do your example with Drop:
I don't know how general this is, but maybe it will help set you off in the right direction.