4

I have no idea how to even begin doing this It needs to be a for loop to multiply mtrixes

for example

[[1,2],[3,4]] * [[3,4],[5,6]]

[1 , 2] , [3 , 4]

[3 , 4] *[5 , 6]

Need help much appreciated I know 90% of dont want to code for me so that's ok

It only needs to be two square matrixes

i'm pretty sure the pattern is looking at it in the list thing

a[1][1]*b[1][1]+a[1][2]*b[2][1] a[1][1]b[1][2]+a[1][2]b[2][2] a[2][1]b[1][1]+a[2][2]b[2][1] a[2][1]b[1][2]+a[2][2]b[2][2] 
5
  • 1
    Could you post what you have first? Commented Sep 3, 2011 at 2:13
  • 1
    This Wikipedia article might help you. Commented Sep 3, 2011 at 2:15
  • all have for now is two lists of the matrixes Commented Sep 3, 2011 at 2:16
  • 2
    Please read stackoverflow.com/questions/how-to-ask Commented Sep 3, 2011 at 2:19
  • For the record, if you need to do this for real (i.e. when you're not studying how it's implemented), NumPy is very good for this sort of thing. Commented Sep 3, 2011 at 5:59

7 Answers 7

4
result = [] # final result for i in range(len(A)): row = [] # the new row in new matrix for j in range(len(B[0])): product = 0 # the new element in the new row for v in range(len(A[i])): product += A[i][v] * B[v][j] row.append(product) # append sum of product into the new row result.append(row) # append the new row into the final result print(result) 
Sign up to request clarification or add additional context in comments.

Comments

2

Break it down. Before you try to write a function that multiplies matrices, write one that multiplies vectors. If you can do that, multiplying two matrices is just a matter of multiplying row i and column j for every element i,j of the resultant matrix.

1 Comment

"Do, or do not. There is no try." -Yoda
2

If you look at how matrix multiplication works:

[ 1 2 ] x [ 5 6 ] = [ 1*5+2*7 1*6+2*8 ] [ 3 4 ] [ 7 8 ] [ 3*5+4*7 3*6+4*8 ] 

then you can determine a method to calculate this, e.g. if you are multiplying for element i, j of the output matrix, then you need to multiply everything in row i of the LHS matrix by everything in the column j of the RHS matrix, so that is a single for loop (as the number of elements in the row i is equal to column j).

You also need to cover every combination of i and j for the dimensions of the output matrix, which is a for loop for the columns nested inside a for loop for the rows.

The actual code is, of course, an exercise for you to implement.

2 Comments

yeah like i edited above i know how to multiply i just need to code using a for loop any ideas or tips on how to start coding
Well if you know how to multiply, then you can code it, right?
1
>>> A=[[1,2],[3,4]] >>> B=[[3,4],[5,6]] >>> n=2 >>> ans=[[0]*n for i in range(n)] >>> ans [[0, 0], [0, 0]] >>> for i in range(n): ... for j in range(n): ... ans[i][j]=sum((A[i][v]*B[v][j] for v in range(n))) ... >>> ans [[13, 16], [29, 36]] 

I think you just need to simplify the formula of matrix multiplication.

We have A*B=C then: Cij= the value in the ith row and jth column of the answer. For example above we have C12=16 and C11=13.. (note that this is the 0th position in the array so often we start from 0 instead of 1)

Cij= dot_product(row_i_of_A,column_j_of_B)=sum(row_i_of_A(v)*column_j_of_B(v) for v in range(n))

Because we want the whole answer (all of C), we need to work out all possible Cij. This means we need to try all possible pairs ij, so we loop through i in range(n), j in range(n) and do this for each possible pair.

Comments

1
from numpy import * m1 = array([[1, 2, 3],[4, 5, 6] ]) m2 = array([[7, 8],[9, 10],[11, 12]]) r = array([[0, 0],[0, 0]]) s = 0 for i in range(2): for j in range(2): for k in range(3): s = s + m1[i][k]*m2[k][j] r[i][j] = s s = 0 print(r) 

I think append function is not working in a two-dimensional array when we are using numpy module, so this is the way I have solved it.

Comments

1
def matmul(matrix1_,matrix2_): result = [] # final result for i in range(len(matrix1_)): row = [] # the new row in new matrix for j in range(len(matrix2_[0])): product = 0 # the new element in the new row for v in range(len(matrix1_[i])): product += matrix1_[i][v] * matrix2_[v][j] row.append(product) # append sum of product into the new row result.append(row) # append the new row into the final result return result 

Comments

0

u and v are constructed for visualization purpose.

from typing import List A = [[1,0,0],[-1,0,3]] B = [[7,0,0],[0,0,0],[0,0,1]] def mult_mat(A:List[List[int]], B:List[List[int]]) -> List[List[int]]: n = len(A) # Number of rows in matrix A m = len(B[0]) # Number of columns in matrix B ret = [[0 for i in range(m)] for j in range(n)] for row in range(n): u = A[row] for col in range(m): v = [B[i][col] for i in range(len(B))] # Here you can calculate ret[row][col] directly without v # But v is constructed for visualization purpose ret[row][col] = sum([x*y for x,y in zip(u,v)]) return ret if __name__ == '__main__': print(mult_mat(A,B)) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.