473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
Jan 28 '07 #1
5 72114
bvdet
2,851 Expert Mod 2GB
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
2,851 Expert Mod 2GB
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
@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
2,851 Expert Mod 2GB
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
391 Expert 256MB
Incidentally numpy has a matrix object that does the above and also supports matrix operations
Jan 3 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: SallyBenjamin | last post by:
Hello.. Can anyone help me with this coding. Basically, it needs to have add node, remove node, add edges , remove edges and display the graph But.I have only succedded to add node, add and...
5
by: mohammaditraders | last post by:
Write a program which overloads a binary Minus (-) operator, The program will contain a class Matrix, This class will contain a private data member Array which store int values. The class will...
7
by: mohammaditraders | last post by:
Write a program which overloads a binary Minus (+) operator, The program will contain a class Matrix, This class will contain a private data member Array which store int values. The class will...
2
by: Zhengzheng Pan | last post by:
Hi all, I'm trying to check whether a (stochastic/transition) matrix converges, i.e. a function/method that will return True if the input matrix sequence shows convergence and False otherwise....
2
by: devnew | last post by:
hi i am looking for some info about mapping btw values in an array and corresponding columns of a matrix i have an numpy array= and a numpy matrix object= matrix((, , , ))
1
by: dazzler | last post by:
Hi! I have problem with numpy, multiplying with an inversed matrix will crash python :( this works fine: from numpy import matrix A = matrix(,]) B = matrix(,]) print A.I #inverse matrix
0
by: harryos | last post by:
hi all I am new to python and learning PCA method by reading up Turk&Petland papers etc while trying out PCA on a set of greyscale images using python, and numpy I tried to create eigenvectors...
0
by: arsyed | last post by:
On Thu, Aug 7, 2008 at 1:36 PM, Simon Parker <simon_ecc@yahoo.co.ukwrote: You can use numpy: http://www.scipy.org/NumPy for example: In : arr = numpy.char.array()
0
by: Gary Herron | last post by:
A. Joseph wrote: Your question was completely understandable, but so incredibly vague as to be a waste of bandwidth to send and a waste of time to answer. What kind of matrix programming do...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.