我认为Mathematica偏向于行而不是列.
给定一个矩阵,插入一行似乎很容易,只需使用 Insert[]
(a = {{1, 2, 3}, {4, 0, 8}, {7 , 8, 0}}) // MatrixForm
1 2 3
4 0 8
7 8 0
row = {97, 98, 99};
(newa = Insert[a, row, 2]) // MatrixForm
1 2 3
97 98 99
4 0 8
7 8 0
Run Code Online (Sandbox Code Playgroud)
但是为了插入一个专栏,经过一番努力之后,我找到了两种方法,我在下面展示,如果他们看到更短更直接的方式,我想问问专家(Mathematica有这么多命令,我可能忽略了一个这种事情以非常直接的方式完成),因为我认为我现在拥有的方法对于这样的基本操作来说仍然过于复杂.
必须做双转置:
a = {{1, 2, 3}, {4, 0, 8}, {7 , 8, 0}}
column = {97, 98, 99}
newa = Transpose[Insert[Transpose[a], column, 2]]
1 97 2 3
4 …Run Code Online (Sandbox Code Playgroud) 我有一个二维列表和一维列表.我想将1D列表作为附加列插入到2D列表中.例如:
array = {{a,1,2},{b,2,3},{c,3,4}};
column = {x,y,z};
Run Code Online (Sandbox Code Playgroud)
变
final = {{a,1,2,x},{b,2,3,y},{c,3,4,z}};
Run Code Online (Sandbox Code Playgroud)
我做得非常好:
Table[Insert[array[[i]], column[[i]], 4], {i, Length[array]}];
Run Code Online (Sandbox Code Playgroud)
我的问题:在Mathematica中这样做的正确方法是什么?我不认为它需要我正在使用的循环.我的解决方案感觉很难看.
好吧,想象一下我有这个矩阵:{{1,2},{2,3}},而我宁愿拥有{{4,1,2},{5,2,3}}.也就是说,我在列中添加了一列.有一个简单的方法吗?
我最好的建议是:
PrependColumn[vector_List, matrix_List] :=
Outer[Prepend[#1, #2] &, matrix, vector, 1]
Run Code Online (Sandbox Code Playgroud)
但它会混淆代码并不断需要加载越来越多的代码.这不是以某种方式建造的吗?