364,111 Members | 2059 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

please how to create a matrix in python??

amine1978
P: 1
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
Jan 28 '07 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,509
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:
Expand|Select|Wrap|Line Numbers
  1. # A simple matrix
  2. # This matrix is a list of lists
  3. # Column and row numbers start with 1
  4.  
  5. class Matrix(object):
  6.     def __init__(self, cols, rows):
  7.         self.cols = cols
  8.         self.rows = rows
  9.         # initialize matrix and fill with zeroes
  10.         self.matrix = []
  11.         for i in range(rows):
  12.             ea_row = []
  13.             for j in range(cols):
  14.                 ea_row.append(0)
  15.             self.matrix.append(ea_row)
  16.  
  17.     def setitem(self, col, row, v):
  18.         self.matrix[col-1][row-1] = v
  19.  
  20.     def getitem(self, col, row):
  21.         return self.matrix[col-1][row-1]
  22.  
  23.     def __repr__(self):
  24.         outStr = ""
  25.         for i in range(self.rows):
  26.             outStr += 'Row %s = %s\n' % (i+1, self.matrix[i])
  27.         return outStr
  28.  
  29.  
  30. a = Matrix(4,4)
  31. print a
  32. a.setitem(3,4,'55.75')
  33. print a
  34. a.setitem(2,3,'19.1')
  35. print a
  36. print a.getitem(3,4)
Output:
Expand|Select|Wrap|Line Numbers
  1. >>> Row 1 = [0, 0, 0, 0]
  2. Row 2 = [0, 0, 0, 0]
  3. Row 3 = [0, 0, 0, 0]
  4. Row 4 = [0, 0, 0, 0]
  5.  
  6. Row 1 = [0, 0, 0, 0]
  7. Row 2 = [0, 0, 0, 0]
  8. Row 3 = [0, 0, 0, '55.75']
  9. Row 4 = [0, 0, 0, 0]
  10.  
  11. Row 1 = [0, 0, 0, 0]
  12. Row 2 = [0, 0, '19.1', 0]
  13. Row 3 = [0, 0, 0, '55.75']
  14. Row 4 = [0, 0, 0, 0]
  15.  
  16. 55.75
  17. >>> 
Jan 28 '07 #2

bvdet
Expert Mod 2.5K+
P: 2,509
Here's an iterator method for 'Matrix':
Expand|Select|Wrap|Line Numbers
  1.     def __iter__(self):
  2.         for row in range(self.rows):
  3.             for col in range(self.cols):
  4.                 yield (self.matrix, row, col)  
Interactive:
Expand|Select|Wrap|Line Numbers
  1. >>> a = Matrix(3,3)
  2. >>> x = 1.5
  3. >>> for i,r,c in a:
  4. ...     x = x*2
  5. ...     i[r][c] = x
  6. ...     
  7. >>> a
  8. Row 1 = [3.0, 6.0, 12.0]
  9. Row 2 = [24.0, 48.0, 96.0]
  10. Row 3 = [192.0, 384.0, 768.0]
  11.  
  12. >>> 
Jan 30 '07 #3

mohit ahuja
P: 1
@bvdet
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
Jan 2 '12 #4

bvdet
Expert Mod 2.5K+
P: 2,509
This should do the trick:
Expand|Select|Wrap|Line Numbers
  1.     def getcolumn(self, col):
  2.         return [self.matrix[col-1][i] for i in range(self.rows)]
Jan 2 '12 #5

Glenton
Expert 100+
P: 380
Incidentally numpy has a matrix object that does the above and also supports matrix operations
Jan 3 '12 #6

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python matrix python python matrix