如何插入额外的所需列和行?

发布于 2025-01-23 07:55:54 字数 1243 浏览 3 评论 0原文

我有一个矩阵

any_matrix = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])

,我想插入所需的列,然后在之间行列,如下(如下所示)

  
[[1,   2,   0,   3,    4,  0   ],
[5,    6,   0,   7,    8,  0   ],
[0,    0,   1,   0,    0,  0   ],
[9,    10,  0,   11,  12,  0   ],
[13,   14,  0,   15,  16,  0   ],
[0,    0,   0,   0,    0,  1   ]]

我的尝试

import numpy as np


any_matrix = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])


desired_cols_ids = np.array([2, 4], dtype=np.int64)
zero_arr_row = np.zeros((1, any_matrix .shape[1]))
any_matrix  = np.insert(any_matrix , desired_cols_ids, zero_arr_row, axis = 0)
zero_arr_col = np.array([0,0,1,0,0,0]).reshape(6,1)
any_matrix  = np.insert(any_matrix , desired_cols_ids, zero_arr_col, axis=1)

print(any_matrix)

>>output

[[ 1  2  0  3  4  0]
 [ 5  6  0  7  8  0]
 [ 0  0  1  0  0  1]
 [ 9 10  0 11 12  0]
 [13 14  0 15 16  0]
 [ 0  0  0  0  0  0]]

i have a matrix

any_matrix = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])

and I want to insert desired column and row in between, like this( shown below )

  
[[1,   2,   0,   3,    4,  0   ],
[5,    6,   0,   7,    8,  0   ],
[0,    0,   1,   0,    0,  0   ],
[9,    10,  0,   11,  12,  0   ],
[13,   14,  0,   15,  16,  0   ],
[0,    0,   0,   0,    0,  1   ]]

my attempt

import numpy as np


any_matrix = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])


desired_cols_ids = np.array([2, 4], dtype=np.int64)
zero_arr_row = np.zeros((1, any_matrix .shape[1]))
any_matrix  = np.insert(any_matrix , desired_cols_ids, zero_arr_row, axis = 0)
zero_arr_col = np.array([0,0,1,0,0,0]).reshape(6,1)
any_matrix  = np.insert(any_matrix , desired_cols_ids, zero_arr_col, axis=1)

print(any_matrix)

>>output

[[ 1  2  0  3  4  0]
 [ 5  6  0  7  8  0]
 [ 0  0  1  0  0  1]
 [ 9 10  0 11 12  0]
 [13 14  0 15 16  0]
 [ 0  0  0  0  0  0]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸠书 2025-01-30 07:55:54

我们必须同时使用np.insert和np.hstack进行此操作,

看起来有些麻烦,但它会起作用!

import numpy as np

any_matrix = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])


desired_cols_ids = np.array([2, 4], dtype=np.int64)
zero_arr_row = np.zeros((1, any_matrix .shape[1]))
any_matrix  = np.insert(any_matrix , desired_cols_ids, zero_arr_row, axis = 0)
print(any_matrix)

# Array to be added as column at the last
column_to_be_added = np.array([0,0,0,0,0,1])
 
# Adding last column to numpy array
any_matrix = np.hstack((any_matrix, np.atleast_2d(column_to_be_added).T))

# Adding column at second position to numpy array
desired_cols_ids_1 = np.array([2,], dtype=np.int64)
zero_arr_col = np.array([0,0,1,0,0,0]).reshape(6,1)
any_matrix = np.insert(any_matrix, desired_cols_ids_1, zero_arr_col, axis=1)                   
                   

print(any_matrix)


we have to use both np.insert and np.hstack for this operation

looks a little cumbersome, but it will work !!

import numpy as np

any_matrix = np.array( [[1,     2,   3,    4   ],
                        [5,     6,   7,    8   ],
                        [9,    10,   11,  12   ],
                        [13,   14,   15,   16  ]])


desired_cols_ids = np.array([2, 4], dtype=np.int64)
zero_arr_row = np.zeros((1, any_matrix .shape[1]))
any_matrix  = np.insert(any_matrix , desired_cols_ids, zero_arr_row, axis = 0)
print(any_matrix)

# Array to be added as column at the last
column_to_be_added = np.array([0,0,0,0,0,1])
 
# Adding last column to numpy array
any_matrix = np.hstack((any_matrix, np.atleast_2d(column_to_be_added).T))

# Adding column at second position to numpy array
desired_cols_ids_1 = np.array([2,], dtype=np.int64)
zero_arr_col = np.array([0,0,1,0,0,0]).reshape(6,1)
any_matrix = np.insert(any_matrix, desired_cols_ids_1, zero_arr_col, axis=1)                   
                   

print(any_matrix)


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文