1,105,399 Community Members

creating a 3 by 4 matrix

Member Avatar
ricepicker417
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I mainly need help with creating the 3 by 4 matrix (part 3 of the assignment) as i am confused as how i would create it. However this is the entire assignment:

  1. Write a function that returns the sum of all the elements in a specified column in a matrix using the following header:

    def sumColumn(matrix, columnIndex)

  2. Write a function that displays the elements in a matrix row by row, where the values in each row are displayed on a separate line (see the output below).

3.Write a test program (i.e., a main function) that reads a 3 X 4 matrix and displays the sum of each column. Here is a sample run:

Enter a 3-by-4 matrix row for row 0: 2.5 3 4 1.5
Enter a 3-by-4 matrix row for row 1: 1.5 4 2 7.5
Enter a 3-by-4 matrix row for row 2: 3.5 1 1 2.5

The matrix is
2.5 3.0 4.0 1.5
1.5 4.0 2.0 7.5
3.5 1.0 1.0 2.5

Sum of elements for column 0 is 7.5
Sum of elements for column 1 is 8.0
Sum of elements for column 2 is 7.0
Sum of elements for column 3 is 11.5

Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Show your code, and tell us what is not clear.

Member Avatar
Lardmeister
Posting Virtuoso
1,966 posts since Mar 2007
Reputation Points: 434 [?]
Q&As Helped to Solve: 111 [?]
Skill Endorsements: 8 [?]
 
0
 

Hint, your matrix will be a list of 3 sublists, each sublist has 4 elements.

matrix34 = [
[2.5, 3.0, 4.0, 1.5],
[1.5, 4.0, 2.0, 7.5],
[3.5, 1.0, 1.0, 2.5]
]

Each sublist forms a row and each column has matching index number elements from each sublist. The index is zero based, so column one has index 0. The value in row three and column one is matrix34[2][0] --> 3.5

Member Avatar
aswekown
Newbie Poster
7 posts since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 

this is easy to do :

#!/usr/bin/env python
#-*- coding:utf-8 -*-

matirx = []
for i in xrange(3):
    print "Enter a 3-by 4 matrix row for row %d" % i
    row = map(float, raw_input().split(' '))
    matirx.append(row)

print "The matrix is"

for row in matirx:
    print row

revmatirx = map(list, zip(*matirx))

for i in xrange(4):
    print "Sum of elements for column %d is" % i
    print sum(revmatirx[i])
Member Avatar
dashing.adamhughes
Light Poster
38 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 1 [?]
 
0
 

Also, if this is just an exercise, then doing it with native python datastructures (like lists) is a nice way to go. In reality, you should use numpy to make a matrix. It's a library for just this, and will be about 1000 times faster than a matrix made out of python datastructures, due to its c-level optimizations.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
1
 
Question Answered as of 1 Year Ago by pyTony, dashing.adamhughes, aswekown and 2 others
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: