矩阵删除、插入、尾部添加操作(delete,insert,append) (1)
程序员文章站
2022-03-01 23:20:51
...
import numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
p1=np.delete(matrix,1,0)
print('>>>>p1>>>>\n',p1)
>>>>p1>>>>
[[ 1 2 3 4]
[ 9 10 11 12]]
p2 = np.delete(matrix, 1, 1)
print('>>>>p2>>>>\n',p2)
>>>>p2>>>>
[[ 1 3 4]
[ 5 7 8]
[ 9 11 12]]
p3 = np.delete(matrix, 1)
print('>>>>p3>>>>\n',p3)
>>>>p3>>>>
[ 1 3 4 5 6 7 8 9 10 11 12]
p4 = np.delete(matrix, [0,1], 1)
print('>>>>p4>>>>\n',p4)
>>>>p4>>>>
[[ 3 4]
[ 7 8]
[11 12]]
import numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
q1 = np.insert(matrix, 1, [1,1,1,1], 0)
print('>>>>q1>>>>\n',q1)
>>>>q1>>>>
[[ 1 2 3 4]
[ 1 1 1 1]
[ 5 6 7 8]
[ 9 10 11 12]]
q2 = np.insert(matrix, 0, [1,1,1], 1)
print('>>>>2>>>>\n',q2)
>>>>2>>>>
[[ 1 1 2 3 4]
[ 1 5 6 7 8]
[ 1 9 10 11 12]]
q3 = np.insert(matrix, 3, [1,1,1,1], 0)
print('>>>>3>>>>\n',q3)
>>>>3>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
mport numpy as np
matrix = [
[1,2,3,4],
[5,6,7,8],
[9,10,11,12]
]
File "<ipython-input-20-d202c3e7f41f>", line 1
mport numpy as np
^
SyntaxError: invalid syntax
m1 = np.append(matrix,[[1,1,1,1]],axis=0)
print('>>>>m1>>>>\n',m1)
m2 = np.append(matrix,[[1],[1],[1]],axis=1)
>>>>m1>>>>
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[ 1 1 1 1]]
print('>>>>m2>>>>\n',m2)
m3 = np.append(matrix,[[1],[1],[1]],axis=1)
>>>>m2>>>>
[[ 1 2 3 4 1]
[ 5 6 7 8 1]
[ 9 10 11 12 1]]
print('>>>>m3>>>>\n',m3)
>>>>m3>>>>
[[ 1 2 3 4 1]
[ 5 6 7 8 1]
[ 9 10 11 12 1]]
上一篇: vector