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
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: - # 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: - >>> 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
-
>>>
bvdet 2,851
Expert Mod 2GB
Here's an iterator method for 'Matrix': - def __iter__(self):
-
for row in range(self.rows):
-
for col in range(self.cols):
-
yield (self.matrix, row, col)
Interactive: - >>> 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]
-
-
>>>
@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
bvdet 2,851
Expert Mod 2GB
This should do the trick: - def getcolumn(self, col):
-
return [self.matrix[col-1][i] for i in range(self.rows)]
Incidentally numpy has a matrix object that does the above and also supports matrix operations
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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....
|
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((,
,
,
))
|
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
|
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...
|
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()
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
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....
|
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...
|
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...
| |