Skip to content

Commit 9f78d6a

Browse files
committed
Removes old matrix multiplication example
1 parent d4ab657 commit 9f78d6a

File tree

1 file changed

+0
-53
lines changed

1 file changed

+0
-53
lines changed

docs/datastructure.rst

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -419,56 +419,3 @@ The output
419419
Babai failed :(
420420
Tesla 's total marks 296
421421
Tesla passed :)
422-
423-
matrixmul.py
424-
============
425-
426-
In this example we will multiply two matrices. First we will take input the number of rows/columns in the matrix (here we assume we are using n x n matrix). Then values of the matrices.
427-
428-
::
429-
430-
#!/usr/bin/env python3
431-
n = int(input("Enter the value of n: "))
432-
print("Enter values for the Matrix A")
433-
a = []
434-
for i in range(0, n):
435-
a.append([int(x) for x in input("").split(" ")])
436-
print("Enter values for the Matrix B")
437-
b = []
438-
for i in range(0, n):
439-
b.append([int(x) for x in input("").split(" ")])
440-
c = []
441-
for i in range(0, n):
442-
c.append([a[i][j] * b[j][i] for j in range(0, n)])
443-
print("After matrix multiplication")
444-
print("-" * 10 * n)
445-
for x in c:
446-
for y in x:
447-
print("%5d" % y, end=' ')
448-
print("")
449-
print("-" * 10 * n)
450-
451-
The output
452-
453-
::
454-
455-
$ ./matrixmul.py
456-
Enter the value of n: 3
457-
Enter values for the Matrix A
458-
1 2 3
459-
4 5 6
460-
7 8 9
461-
Enter values for the Matrix B
462-
9 8 7
463-
6 5 4
464-
3 2 1
465-
After matrix multiplication
466-
------------------------------
467-
9 12 9
468-
32 25 12
469-
49 32 9
470-
------------------------------
471-
472-
Here we have used list comprehensions couple of times. *[int(x) for x in input("").split(" ")]* here first it takes the input as string by *input()*, then split the result by " ", then for each value create one int. We are also using *[a[i][j] * b[j][i] for j in range(0,n)]* to get the resultant row in a single line.
473-
474-

0 commit comments

Comments
 (0)