Question:
matrices and python programming help??
anonymous
2008-02-08 00:00:02 UTC
so I need to write a program that adds matrices using the python language. Can someone help me get started?? I've come up with a pseudo code only I have a lot of trouble converting my pseudo code to python code. If someone could help, it would be fantastic, or if someone knows of a reference that helps convert pseudo code to python. I am a programming and python newbie and I am struggling.. here is my pseudocode :
define a function matrixAdd(A,B) that implements the following...
* Create a blank new matrix, initialized to all 0.
* Outer loop to iterate over each row i
* Inner loop to iterate over each column j of row i
* Add the corresponding A[i][j] and B[i][j], and store the sum in the new matrix
* After exiting the outer loop, return the new sum matrix

so far I have this

def matrixAdd(A,B):
m=[[0]*i]*j
for row in range(0,i):
I don't even know if this is right...any help would be greatly appreciated. Thanks.
Three answers:
okonomiyaki
2008-02-11 15:43:52 UTC
It looks like you're on the right track, but there are a few things.



Are "i" and "j" set to the sizes of the matrices' rows and columns?



You probably want to use a list comprehension. Like this:

m = [[0]*i for column in range(j)]



or like this:

m = [[0]*rowSize for column in range(columnSize)]



Otherwise, "m" will act in very strange ways when you try to store or read values in it, because m will be a list of references to a single "i" long list.



It can be dead frustrating to study programming, but the best pseudocode converter is your brain. Keep at it. :)
?
2016-05-25 14:35:54 UTC
$500 and the program is yours. You can't expect someone to do your work for you for nothing.
anonymous
2008-02-08 00:05:10 UTC
print 'I'll star the question, but I have no idea'



x='sorry'

print x



a=70

b=30



print a/b


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...