numpy拼接矩阵的实现

作者:头戴面包圈 时间:2021-02-09 06:35:07 

1、文档

使用numpy的 concatenate 拼接矩阵,文档里面这样解释:

numpy.concatenate((a1, a2, ...), axis=0, out=None, dtype=None, casting="same_kind")

(a1, a2, ...):连接的数组必须有一样的维度;

axis:拼接的方向;

out:预设输出矩阵的大小

…………

2、举例

首先给定两个矩阵:

rotation = np.array([[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]])

trans = np.array([[7],
                 [8],
                 [0]])

①:在第一个矩阵后面加上第二个矩阵(加一列):

z = np.concatenate((rotation, trans), axis=1)

输出为:

[[1 2 3 7]
 [4 5 6 8]
 [7 8 9 0]]

②:以及在矩阵下面加一个全零行:

add_arr = np.array([[0, 0, 0, 0]])
array_by_add = np.concatenate((z, add_arr), axis=0)

输出:

[[1 2 3 7]
 [4 5 6 8]
 [7 8 9 0]
 [0 0 0 0]]

③:把矩阵填充为对角矩阵:

第一个为3×3矩阵,第二个为2×3矩阵。

arr1 = np.array([[2, 3, 4],
                [3, 4, 5],
                [4, 5, 6]])
arr2 = np.array([[7, 8, 9],
                [8, 9, 10]])

arr_zeros1 = np.zeros((arr2.shape[1], arr1.shape[0]))
print(arr_zeros1)
arr_zeros2 = np.zeros((arr2.shape[0], arr1.shape[1]))
print(arr_zeros2)
total_arr1 = np.concatenate((arr1, arr_zeros1), axis=1)
total_arr2 = np.concatenate((arr_zeros2, arr2,), axis=1)
total_arr = np.concatenate((total_arr1, total_arr2), axis=0)
print(total_arr)

拼接之后结果如下:

[5 6]
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
[[0. 0. 0.]
 [0. 0. 0.]]
[[ 2.  3.  4.  0.  0.  0.]
 [ 3.  4.  5.  0.  0.  0.]
 [ 4.  5.  6.  0.  0.  0.]
 [ 0.  0.  0.  7.  8.  9.]
 [ 0.  0.  0.  8.  9. 10.]]

官方文档地址:numpy官网地址

来源:https://blog.csdn.net/Arhonbt/article/details/125746727

标签:numpy,拼接,矩阵
0
投稿

猜你喜欢

  • mysql 5.7.13 安装配置方法图文教程(win10)

    2024-01-14 06:51:48
  • 任意定制文本对齐方式:CSS Text Wrapper

    2008-02-03 11:11:00
  • python光学仿真学习wxpython创建手速测试程序

    2023-12-06 20:57:28
  • python内置函数之eval函数详解

    2022-07-22 12:39:29
  • 教你如何使用Python开发一个钉钉群应答机器人

    2023-08-03 17:44:16
  • Git常用命令介绍

    2022-08-14 03:42:46
  • mysql 超大数据/表管理技巧

    2024-01-16 22:14:05
  • Oracle DBA常用语句

    2009-08-05 20:15:00
  • java编写创建数据库和表的程序

    2024-01-19 17:24:40
  • python使用pymysql实现操作mysql

    2024-01-15 17:32:17
  • Python图像处理之图像的灰度线性变换

    2021-12-16 22:30:58
  • CKEditor/FCKEditor 使用 CKeditor 3.0.1 快速使用教程(含插入图片)

    2022-10-16 14:04:14
  • 白鸦:内容设计,初始内容

    2008-03-04 16:23:00
  • 一文详解Python中多进程和进程池的使用方法

    2023-12-01 04:10:12
  • python实现简单的井字棋小游戏

    2022-06-30 10:48:05
  • Request.Servervariables(“HTTP_USER_AGENT“)是什么意思。

    2009-08-21 13:13:00
  • TensorFlow实现Logistic回归

    2023-11-27 18:49:14
  • Ubuntu20.04环境安装tensorflow2的方法步骤

    2023-07-04 06:41:21
  • python logging设置level失败的解决方法

    2022-03-23 13:54:40
  • 教你如何使用Python下载B站视频的详细教程

    2023-04-12 00:39:48
  • asp之家 网络编程 m.aspxhome.com