[Tutor] matrix q?
Bob Gailer
bgailer@alum.rpi.edu
Sat Jun 7 13:10:10 2003
At 04:36 AM 6/7/2003 +0000, you wrote:
>i want to add the row of a square matrix.
>
>the code i have is :
>
>myMatrix = [2,7,6], [9,5,1], [4,3,8]
>
>def sumRow(aMatrix, index):
> sum = 0
> for item in aMatrix[index]:
> sum = sum + item
> print aMatrix[index]
> return sum
>
>#print sumRow(myMatrix,len(myMatrix)-1)
>print sumRow(myMatrix,0)
>#print sumRow(myMatrix,1)
>
>
>
>Changing from 0 to 1 and len(myMatrix)-1 i can get the sum of the row each
>time but i want to put the in a loop so i can get the sum of all row
>indevidual the same time.
import operator
myMatrix = [2,7,6], [9,5,1], [4,3,8]
print [reduce(operator.add,i) for i in myMatrix]
# result: [15, 15, 15]
Bob Gailer
bgailer@alum.rpi.edu
303 442 2625