'How to write random matrix using loops and without numpy
How can I create two matrixes without numpy
as A
and B
(can also do these operations)?
import numpy as np
A = np.random.randint(-9, 9, size=(5, 5))
B = np.random.randint(-9, 9, size=(5, 5))
for i in range(5):
print(A[i])
print()
for i in range(5):
print(B[i])
print()
C = A * 2
for i in range(5):
print(C[i])
result = [[A[j][i] for j in range(len(A))] for i in range(len(A[0]))]
print()
for r in result:
print(r)
E = A + B
print()
for i in range(5):
print(E[i])
F = A * B
print()
for i in range(5):
print(B[i])
I tried like so, but in this case [C=A*2] gives me not multiply otherwise it writes 2 times matrix
import random
A = []
size_x, size_y = 5, 5
for _ in range(size_y):
row = []
for _ in range(size_x):
row.append(random.randint(-9, 9))
A.append(row)
B = []
size_x, size_y = 5, 5
for _ in range(size_y):
row = []
for _ in range(size_x):
row.append(random.randint(-9, 9))
B.append(row)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|