如何在矩阵单元格中插入数据数组

时间:2018-07-18 15:29:16

标签: python arrays database numpy matrix

在下面显示的代码中,我试图将一些数据插入矩阵的某些单元格,然后必须使用时间迭代来修改这些数据。 有没有更实用,更优雅的方式?谢谢大家

import numpy as np
import matplotlib.pyplot as plt

class Matrix2D:
    def __init__(self):

    self.matrix = []
    self.rows = 0
    self.columns = 0
    self.name = 'Unnamed'

def __str__(self):
    return self.matrix

def generate(self, rows, columns, verbose=False):
    self.rows = rows
    self.columns = columns
    self.matrix = np.zeros((rows,columns))
    if verbose ==  True:
        print(f'Generata una matrice {self.rows} righe per {self.columns} colonne')
        print('--------' * columns)
        self.printme()
        print('--------' * columns)
    return self.matrix

def printme(self, verbose=False):
    if verbose == True:
        print(f'Una matrice {self.rows} righe per {self.columns} colonne    \n')
    for row in self.matrix:
        print(row)

def get_cell(self, row, col, verbose=False):

    if verbose ==  True:
        print(f'\ncell[row={row}, col={col}] :')
        print(self.matrix[row][col])
    return self.matrix[row][col]

def intorno(self,row,col):
    I=np.array([[row+1,col],[row+1,col-1],[row+1,col+1]])
    return I

def seed(self,row,col,data,verbose=False):
    self.matrix[row][col]= data

    return self.matrix[row][col]


print('***************')
m = Matrix2D()
m.generate(10,10,True)
#m.get_cell(2,2,True)

init_state=np.array((5.0,4.0))   #data array 


for i in range (3,7):
    m.seed(1,i,init_state, True)

m.printme(True)

我想得到一个矩阵,可以在其中控制单个单元格的多个值

示例:

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. (data) (data) (data) (data) 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

0 个答案:

没有答案