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 -
代码解析:
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()
- 结果:
**************************************************
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 ; - 代码:
#!/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()
- 结果:
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.]]