Hey
I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the code I have (I am using Python):
[CODE=python]
def generate (rows, cols): # This just prints the coordinates and the number that goes in it, It also prints the matrix in a square
import random
m = {}
for r in range(rows):
for c in range(cols):
m[r,c]=random.randran ge(100)
print m
print "\n"
for r in range(rows):
for c in range (cols):
print str(m[r,c]).rjust(4),
print
return m
def rowsum(m): # This is summing the row
results = []
for row in len(m):
results=sum(m(r ow)) # I think this is were my problem is
return results
def columnsum(m): # This is summing the column
rows = len(m)
cols = len(m[0]) # I think this is were my other problem is
result = []
for c in range(cols):
sum = 0
for r in range(rows):
sum += m[r][c]
result.append(s um)
return result
def diagonal(m): # This shows the numbers from the top left to bottom right diagonal
diagonal = []
rows = len(m)
for row in range(rows):
diagonal.append (m[row][row]) # I think this is were my final problem is
return diagonal
[/CODE]
The error I am getting is about my rows, columns and diagonals being strings and not numbers. I can do all the summing of the rows and columns and show the numbers in the diagonal just fine using lists. I know the dictionary form of summing the rows and columns and showing the numbers in the diagonal are very similar to the list form, but for the love of me I just can't figure it out. If someone has any ideas on how to best sum the rows and columns and show the numbers in the diagonal the help would be appreciated.
I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the code I have (I am using Python):
[CODE=python]
def generate (rows, cols): # This just prints the coordinates and the number that goes in it, It also prints the matrix in a square
import random
m = {}
for r in range(rows):
for c in range(cols):
m[r,c]=random.randran ge(100)
print m
print "\n"
for r in range(rows):
for c in range (cols):
print str(m[r,c]).rjust(4),
return m
def rowsum(m): # This is summing the row
results = []
for row in len(m):
results=sum(m(r ow)) # I think this is were my problem is
return results
def columnsum(m): # This is summing the column
rows = len(m)
cols = len(m[0]) # I think this is were my other problem is
result = []
for c in range(cols):
sum = 0
for r in range(rows):
sum += m[r][c]
result.append(s um)
return result
def diagonal(m): # This shows the numbers from the top left to bottom right diagonal
diagonal = []
rows = len(m)
for row in range(rows):
diagonal.append (m[row][row]) # I think this is were my final problem is
return diagonal
[/CODE]
The error I am getting is about my rows, columns and diagonals being strings and not numbers. I can do all the summing of the rows and columns and show the numbers in the diagonal just fine using lists. I know the dictionary form of summing the rows and columns and showing the numbers in the diagonal are very similar to the list form, but for the love of me I just can't figure it out. If someone has any ideas on how to best sum the rows and columns and show the numbers in the diagonal the help would be appreciated.
Comment