multiply two matrices

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nilushika
    New Member
    • Sep 2013
    • 20

    multiply two matrices

    python
    Code:
    def readmat(r,c):
        for i in range(r):
            list=[]
            str=raw_input()
            templist=str.split()
            raw=map(int,templist)
            list.append(raw)
        return list
    
    def product():
        list=[]
        for i in matA:
            sublist=[]
            print i[3],matB[1][1]
            for x in range(3):
                sum=0
                for e in range(4):
                    sum+=i[e]*matB[e][x]
                sublist.append(sum)
            list.append(sublist)
        return list
    
    def display(list):
        for r in list:
            for c in r:
                print c,
            print''
    
    print 'enter A'
    matA=readmat(3,4)
    print 'enter B'
    matB=readmat(4,3)
    display(product())
    why it say that list index out of bound???there is an error in product(),can u please help me?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Please don't use Python builtin types for identifiers such as str and list.

    You received the error because you have a list of lists (matA) and each element is only one element long, I think. I can't be sure what you expect to enter into the raw_input widget.

    Comment

    Working...