提取n-1矩阵和增加一行一列

1. 增加一行一列

  • 假设我们有一个nxn的矩阵,如何提取下(n-1)x(n-1)矩阵,举例说明:
    A = 1 2 3 4 5 6 7 8 9 → B = 5 6 8 9 \begin{equation} A = \begin{bmatrix} 1&2&3\\\\ 4&5&6\\\\ 7&8&9\end{bmatrix}\to B=\begin{bmatrix} 5&6\\\\ 8&9\end{bmatrix} \end{equation} A= 147258369 →B= 5869

  • 假设我们有一个nxn的矩阵,增加一行零和一列零,组成(n+1)x(n+1)矩阵,举例说明:
    A = 1 2 3 4 5 6 7 8 9 → D = 0 0 0 0 0 1 2 3 0 4 5 6 0 7 8 9 \begin{equation} A = \begin{bmatrix} 1&2&3\\\\ 4&5&6\\\\ 7&8&9\end{bmatrix}\to D=\begin{bmatrix} 0&0&0&0\\\\ 0&1&2&3\\\\ 0&4&5&6\\\\ 0&7&8&9\end{bmatrix} \end{equation} A= 147258369 →D= 0000014702580369

  • 代码解析:

python 复制代码
import numpy as np

np.set_printoptions(suppress=True, precision=3)

np.random.seed(20241031)


class MatrixTake(object):
    def __init__(self, in_matrix):
        self.n_matrix = in_matrix
        self.row, self.column = np.shape(self.n_matrix)
        self.n_1_matrix = np.zeros((self.row, self.column))

    def get_n_1_matrix(self):
        row_n_1 = self.row - 1
        column_n_1 = self.column - 1
        ones_vector = np.ones(self.row)
        ones_vector[0] = 0
        ones_matrix1 = np.diag(ones_vector)
        ones_matrix2 = ones_matrix1[:, 1:]
        ones_matrix2_t = ones_matrix2.T
        result = ones_matrix2_t @ self.n_matrix @ ones_matrix2
        print(f"*" * 50)
        print(f"matrix=\n{self.n_matrix}")
        print(f"result=\n{result}")
        print(f"*" * 50)


class MatrixAdd(object):
    def __init__(self, in_matrix):
        self.n_matrix = in_matrix
        self.row, self.column = np.shape(self.n_matrix)
        self.n_plus_matrix = np.zeros((self.row + 1, self.column + 1))

    def get_n_plus_matrix(self):
        zeros_matrix = np.zeros((1,self.row))
        eye_matrix1 = np.eye(self.row)
        one_new_matrix = np.row_stack((zeros_matrix, eye_matrix1))
        one_new_matrix_t = one_new_matrix.T
        n_plus_matrix = one_new_matrix @ self.n_matrix @ one_new_matrix_t
        print("%"*50)
        print(f"n_matrix=\n{self.n_matrix}")
        print(f"n_plus_matrix=\n{n_plus_matrix}")
        print("%"*50)


if __name__ == "__main__":
    my_code = 0
    for i in range(5):
        my_matrix = np.random.randint(1, 10, (5, 5))
        my_take = MatrixTake(my_matrix)
        my_take.get_n_1_matrix()
        my_add = MatrixAdd(my_matrix)
        my_add.get_n_plus_matrix()
  • 结果:
python 复制代码
**************************************************
matrix=
[[8 2 5 8 6]
 [9 6 1 4 9]
 [9 6 6 5 3]
 [9 7 6 6 2]
 [8 8 7 6 5]]
result=
[[6. 1. 4. 9.]
 [6. 6. 5. 3.]
 [7. 6. 6. 2.]
 [8. 7. 6. 5.]]
**************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_matrix=
[[8 2 5 8 6]
 [9 6 1 4 9]
 [9 6 6 5 3]
 [9 7 6 6 2]
 [8 8 7 6 5]]
n_plus_matrix=
[[0. 0. 0. 0. 0. 0.]
 [0. 8. 2. 5. 8. 6.]
 [0. 9. 6. 1. 4. 9.]
 [0. 9. 6. 6. 5. 3.]
 [0. 9. 7. 6. 6. 2.]
 [0. 8. 8. 7. 6. 5.]]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
**************************************************
matrix=
[[7 6 7 3 2]
 [7 1 9 9 5]
 [1 8 5 3 9]
 [8 5 8 8 9]
 [9 7 7 6 4]]
result=
[[1. 9. 9. 5.]
 [8. 5. 3. 9.]
 [5. 8. 8. 9.]
 [7. 7. 6. 4.]]
**************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_matrix=
[[7 6 7 3 2]
 [7 1 9 9 5]
 [1 8 5 3 9]
 [8 5 8 8 9]
 [9 7 7 6 4]]
n_plus_matrix=
[[0. 0. 0. 0. 0. 0.]
 [0. 7. 6. 7. 3. 2.]
 [0. 7. 1. 9. 9. 5.]
 [0. 1. 8. 5. 3. 9.]
 [0. 8. 5. 8. 8. 9.]
 [0. 9. 7. 7. 6. 4.]]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
**************************************************
matrix=
[[4 1 4 1 5]
 [3 3 8 6 4]
 [5 7 6 2 2]
 [4 3 8 8 6]
 [7 8 1 4 6]]
result=
[[3. 8. 6. 4.]
 [7. 6. 2. 2.]
 [3. 8. 8. 6.]
 [8. 1. 4. 6.]]
**************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_matrix=
[[4 1 4 1 5]
 [3 3 8 6 4]
 [5 7 6 2 2]
 [4 3 8 8 6]
 [7 8 1 4 6]]
n_plus_matrix=
[[0. 0. 0. 0. 0. 0.]
 [0. 4. 1. 4. 1. 5.]
 [0. 3. 3. 8. 6. 4.]
 [0. 5. 7. 6. 2. 2.]
 [0. 4. 3. 8. 8. 6.]
 [0. 7. 8. 1. 4. 6.]]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
**************************************************
matrix=
[[2 1 2 3 9]
 [9 9 7 4 5]
 [2 4 4 9 3]
 [7 2 5 4 6]
 [7 8 1 9 8]]
result=
[[9. 7. 4. 5.]
 [4. 4. 9. 3.]
 [2. 5. 4. 6.]
 [8. 1. 9. 8.]]
**************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_matrix=
[[2 1 2 3 9]
 [9 9 7 4 5]
 [2 4 4 9 3]
 [7 2 5 4 6]
 [7 8 1 9 8]]
n_plus_matrix=
[[0. 0. 0. 0. 0. 0.]
 [0. 2. 1. 2. 3. 9.]
 [0. 9. 9. 7. 4. 5.]
 [0. 2. 4. 4. 9. 3.]
 [0. 7. 2. 5. 4. 6.]
 [0. 7. 8. 1. 9. 8.]]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
**************************************************
matrix=
[[2 1 5 2 2]
 [8 8 2 2 1]
 [4 7 9 5 9]
 [1 4 3 4 6]
 [3 6 3 9 7]]
result=
[[8. 2. 2. 1.]
 [7. 9. 5. 9.]
 [4. 3. 4. 6.]
 [6. 3. 9. 7.]]
**************************************************
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
n_matrix=
[[2 1 5 2 2]
 [8 8 2 2 1]
 [4 7 9 5 9]
 [1 4 3 4 6]
 [3 6 3 9 7]]
n_plus_matrix=
[[0. 0. 0. 0. 0. 0.]
 [0. 2. 1. 5. 2. 2.]
 [0. 8. 8. 2. 2. 1.]
 [0. 4. 7. 9. 5. 9.]
 [0. 1. 4. 3. 4. 6.]
 [0. 3. 6. 3. 9. 7.]]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

2. 增加行向量

  • 用矩阵乘法的形式增加行向量
    A = 1 2 3 4 5 6 7 8 9 ; v = 888 444 666 → E = 1 2 3 4 5 6 7 8 9 888 444 666 ; \begin{equation} A = \begin{bmatrix} 1&2&3\\\\ 4&5&6\\\\ 7&8&9\end{bmatrix};v=\begin{bmatrix} 888&444&666 \end{bmatrix}\to E=\begin{bmatrix} 1&2&3\\\\ 4&5&6\\\\ 7&8&9\\\\ 888&444&666\end{bmatrix}; \end{equation} A= 147258369 ;v=888444666→E= 147888258444369666 ;
  • 代码:
python 复制代码
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :MatrixAdd.py
# @Time      :2024/10/31 20:07
# @Author    :Jason Zhang
import numpy as np

np.set_printoptions(suppress=True, precision=3)


class MatrixAddVector(object):
    def __init__(self, in_matrix, in_vector):
        self.in_matrix = in_matrix
        self.row, self.column = np.shape(self.in_matrix)
        self.in_vector = in_vector
        self.result = np.zeros((self.row + 1, self.column))

    def add_vector(self):
        my_vector = self.in_vector
        my_matrix = self.in_matrix
        my_row_1 = self.row + 1
        my_column = self.column
        left_ones = np.ones(my_row_1)
        left_ones_diag = np.diag(left_ones)
        left_matrix = left_ones_diag[:, :-1]
        left_matrix_1 = left_matrix @ self.in_matrix
        zeros_vector = np.zeros(my_row_1)
        zeros_vector[-1] = 1
        add_matrix = np.outer(zeros_vector, self.in_vector)
        result = add_matrix + left_matrix_1
        print(f"matrix=\n{self.in_matrix}")
        print(f"vector={self.in_vector}")
        print(f"result=\n{result}")


if __name__ == "__main__":
    run_code = 0
    new_matrix = np.arange(9).reshape(3, 3)+1
    new_vector = np.array([888, 444, 666])
    new_test = MatrixAddVector(new_matrix, new_vector)
    new_test.add_vector()
  • 结果:
python 复制代码
matrix=
[[1 2 3]
 [4 5 6]
 [7 8 9]]
vector=[888 444 666]
result=
[[  1.   2.   3.]
 [  4.   5.   6.]
 [  7.   8.   9.]
 [888. 444. 666.]]
相关推荐
蝶依斓(湖南)全案整装5 小时前
整装行业长尾需求内容矩阵搭建方法:从五个维度拆解用户需求
大数据·人工智能·矩阵
梦帮科技5 小时前
推测解码(Speculative Decoding)与 Draft-Target 双核协同:接受率判据、树状验证与两倍吞吐无损加速
网络·人工智能·深度学习·神经网络·线性代数·矩阵·架构
xqqxqxxq1 天前
LeetCode Hot100 矩阵专题题解笔记
笔记·leetcode·矩阵
Dylan的码园1 天前
求平均成绩 矩阵数据处理 【循环处理 二维数组】
c++·线性代数·矩阵
苏打豆2 天前
CRISP源码阅读——基于ROS2力矩反馈控制器与速度加速度零空间
c++·线性代数·算法·矩阵·机器人
梦帮科技2 天前
LoRA / QLoRA 低秩矩阵流形更新:SVD 分解、秩与缩放因子 α 的物理意义与梯度稳定性保障
网络·深度学习·神经网络·线性代数·矩阵·架构·数据挖掘
苏打豆3 天前
Pinocchio源码阅读——刚体运动物理量空间变换及求导
c++·线性代数·算法·矩阵·机器人
梦帮科技3 天前
量子张量网络破局大模型:从矩阵乘积态 (MPS) 到张量列 (TT-SVD) 低秩收缩全推导
网络·数据结构·数据库·线性代数·矩阵·架构·模拟退火算法
xailorliu3 天前
矩阵定性-正定负定不定半正定半负定
c++·opencv·线性代数·计算机视觉·矩阵
代数狂人3 天前
机器学习数学基础──第 4 章 矩阵:数据的仓库与批量计算的引擎
人工智能·机器学习·矩阵