提取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.]]
相关推荐
武子康23 分钟前
大数据-207 数据挖掘 机器学习理论 - 多重共线性 矩阵满秩 线性回归算法
大数据·人工智能·算法·决策树·机器学习·矩阵·数据挖掘
玛卡巴卡(努力学习版)2 小时前
矩阵特殊打印方式
c++·算法·矩阵
sz66cm6 小时前
数学基础 -- 线性代数之线性无关
人工智能·线性代数·机器学习
herobrineAC13 小时前
线代的几何意义(一)——向量,坐标,矩阵
线性代数·矩阵
Ricciflows20 小时前
分析学大师Elias M. Stein的分析系列教材
线性代数·数学建模·矩阵·概率论·抽象代数·拓扑学·傅立叶分析
亚图跨际21 小时前
MATLAB细胞干扰素信号矩阵和微分方程计算分析
矩阵·常微分方程·化学反应·细胞信号·细胞决策过程·质量作用动力学·平衡流形
余~185381628001 天前
矩阵NFC碰一碰发视频源码开发技术解析,支持OEM
大数据·人工智能·线性代数·矩阵·音视频
好想有猫猫2 天前
【51单片机】矩阵键盘
单片机·嵌入式硬件·矩阵·计算机外设·51单片机·学习方法
pen-ai2 天前
【机器学习】 16. 降维:PCA-主成分分析 Principle Component Analysis
人工智能·机器学习·矩阵·数据挖掘
羞儿2 天前
构建旋转变换矩阵对二维到高维空间的线段点进行旋转
图像处理·人工智能·线性代数·矩阵