please how to create a matrix in python??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amine1978
    New Member
    • Jan 2007
    • 1

    please how to create a matrix in python??

    please I would like to write a fonction that takes as argument a matrix, 2 integers describing a cell coordinate and a value that set the corresponding cell to the value?????????
    please help me
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by amine1978
    please I would like to write a fonction that takes as argument a matrix, 2 integers describing a cell coordinate and a value that set the corresponding cell to the value?????????
    please help me
    Maybe this will help:
    Code:
    # A simple matrix
    # This matrix is a list of lists
    # Column and row numbers start with 1
    
    class Matrix(object):
        def __init__(self, cols, rows):
            self.cols = cols
            self.rows = rows
            # initialize matrix and fill with zeroes
            self.matrix = []
            for i in range(rows):
                ea_row = []
                for j in range(cols):
                    ea_row.append(0)
                self.matrix.append(ea_row)
    
        def setitem(self, col, row, v):
            self.matrix[col-1][row-1] = v
    
        def getitem(self, col, row):
            return self.matrix[col-1][row-1]
    
        def __repr__(self):
            outStr = ""
            for i in range(self.rows):
                outStr += 'Row %s = %s\n' % (i+1, self.matrix[i])
            return outStr
    
    
    a = Matrix(4,4)
    print a
    a.setitem(3,4,'55.75')
    print a
    a.setitem(2,3,'19.1')
    print a
    print a.getitem(3,4)
    Output:
    Code:
    >>> Row 1 = [0, 0, 0, 0]
    Row 2 = [0, 0, 0, 0]
    Row 3 = [0, 0, 0, 0]
    Row 4 = [0, 0, 0, 0]
    
    Row 1 = [0, 0, 0, 0]
    Row 2 = [0, 0, 0, 0]
    Row 3 = [0, 0, 0, '55.75']
    Row 4 = [0, 0, 0, 0]
    
    Row 1 = [0, 0, 0, 0]
    Row 2 = [0, 0, '19.1', 0]
    Row 3 = [0, 0, 0, '55.75']
    Row 4 = [0, 0, 0, 0]
    
    55.75
    >>>

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Here's an iterator method for 'Matrix':
      Code:
          def __iter__(self):
              for row in range(self.rows):
                  for col in range(self.cols):
                      yield (self.matrix, row, col)
      Interactive:
      Code:
      >>> a = Matrix(3,3)
      >>> x = 1.5
      >>> for i,r,c in a:
      ... 	x = x*2
      ... 	i[r][c] = x
      ... 	
      >>> a
      Row 1 = [3.0, 6.0, 12.0]
      Row 2 = [24.0, 48.0, 96.0]
      Row 3 = [192.0, 384.0, 768.0]
      
      >>>

      Comment

      • mohit ahuja
        New Member
        • Jan 2012
        • 1

        #4
        Originally posted by bvdet
        Here's an iterator method for 'Matrix':
        Code:
            def __iter__(self):
                for row in range(self.rows):
                    for col in range(self.cols):
                        yield (self.matrix, row, col)
        Interactive:
        Code:
        >>> a = Matrix(3,3)
        >>> x = 1.5
        >>> for i,r,c in a:
        ... 	x = x*2
        ... 	i[r][c] = x
        ... 	
        >>> a
        Row 1 = [3.0, 6.0, 12.0]
        Row 2 = [24.0, 48.0, 96.0]
        Row 3 = [192.0, 384.0, 768.0]
        
        >>>
        I want to do something related to the columns of a matrix, I am using the same class as created, kindly help me with a function to get i-th column

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          This should do the trick:
          Code:
              def getcolumn(self, col):
                  return [self.matrix[col-1][i] for i in range(self.rows)]

          Comment

          • Glenton
            Recognized Expert Contributor
            • Nov 2008
            • 391

            #6
            Incidentally numpy has a matrix object that does the above and also supports matrix operations

            Comment

            Working...