文章目录
- 1. 随机打乱矩阵的行和列
- 2. python 代码
1. 随机打乱矩阵的行和列
**************************************************
matrix=
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
row_list=[3 1 0 2]
rand_row_matrix=
[[15 16 17 18 19]
[ 5 6 7 8 9]
[ 0 1 2 3 4]
[10 11 12 13 14]]
column_list=[1 2 4 0 3]
rand_column_matrix=
[[ 1. 2. 4. 0. 3.]
[ 6. 7. 9. 5. 8.]
[11. 12. 14. 10. 13.]
[16. 17. 19. 15. 18.]]
**************************************************
2. python 代码
import numpy as np
np.set_printoptions(suppress=True, precision=3)
np.random.seed(2324)
class RandomMatrix(object):
def __init__(self, in_matrix):
self.matrix = in_matrix
self.row, self.column = np.shape(self.matrix)
self.rand_row_list = np.zeros(self.row)
self.rand_column_list = np.zeros(self.column)
self._rand_column_matrix = np.zeros_like(self.matrix)
self._rand_row_matrix = np.zeros_like(self.matrix)
@property
def rand_row_matrix(self):
my_row_n = self.row
rand_row = np.random.permutation(my_row_n)
self.rand_row_list = rand_row
my_rand_row_matrix = self.matrix[rand_row]
self._rand_row_matrix = my_rand_row_matrix
return self._rand_row_matrix
@property
def rand_column_matrix(self):
my_column_n = self.column
rand_column = np.random.permutation(my_column_n)
self.rand_column_list = rand_column
rand_ones = np.eye(my_column_n)[rand_column].T
my_rand_column_matrix = self.matrix @ rand_ones
self._rand_column_matrix = my_rand_column_matrix
return self._rand_column_matrix
def execute(self):
ex_matrix = self.matrix
ex_rand_row_matrix = self.rand_row_matrix
ex_rand_column_matrix = self.rand_column_matrix