Sie sind auf Seite 1von 4

Suma de matrices

1 matriz1=[]
2 matriz2=[]
3 matrizResultado=[]
4
5 print("Matriz 3*3")
6 for a in range(3):
7 matriz1.append([])
8 for b in range(3):
9 matriz1[a].append(input("Ingresar valor de la primera matriz: "))
10 for c in range(3):
11 matriz2.append([])
12 for d in range(3:
13 matriz2[c].append(input("Ingresar valor de la segunda
matriz:"))
14
15 sumar= matriz1 + matriz2
16 matrizResultado=(sumar)
17 print(matrizResultado)

Multiplicación

# Program to multiply two matrices using nested loops

# 3x3 matrix
X = [[12,7,3],
[4 ,5,6],
[7 ,8,9]]
# 3x4 matrix
Y = [[5,8,1,2],
[6,7,3,0],
[4,5,9,1]]
# result is 3x4
result = [[0,0,0,0],
[0,0,0,0],
[0,0,0,0]]

# iterate through rows of X


for i in range(len(X)):
# iterate through columns of Y
for j in range(len(Y[0])):
# iterate through rows of Y
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]

for r in result:
print(r)

#output:
[114, 160, 60, 27]
[74, 97, 73, 14]
[119, 157, 112, 23]
Transpuesta

1. # Transpose of a matrix
2. # Shell Root
3. # 2010
4.
5.
6. #Definition and setting of variables
7. M1 = [[1, 2, 3], [2, 12, 6], [1, 0, -3], [10, -1, 0]]
8. M2 = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
9.
10. for i in range(4):
11. for j in range(3):
12. M2[j][i] = M1[i][j]
13.
14.
15. for i in range(3):
16. print(M2[i])

resultado

1. alex@shellroot:~/Escritorio$ python PoC.py


2. [1, 2, 1, 10]
3. [2, 12, 0, -1]
4. [3, 6, -3, 0]

Suma y resta parte 2

In [18]:# Ejemplo en Python


A = np.array([[1, 3, 2],
[1, 0, 0],
[1, 2, 2]])

B = np.array([[1, 0, 5],
[7, 5, 0],
[2, 1, 1]])
In [19]: # suma de las matrices A y B
A + B
Out[19]: array([[2, 3, 7],
[8, 5, 0],
[3, 3, 3]])
In [20]: # resta de matrices
A - B
Out[20]: array([[ 0, 3, -3],
[-6, -5, 0],
[-1, 1, 1]])
In [21]:# multiplicando matrices por escalares
A * 2
Out[21]:array([[2, 6, 4],
[2, 0, 0],
[2, 4, 4]])
In [22]:B * 3
Out[22]:array([[ 3, 0, 15],
[21, 15, 0],
[ 6, 3, 3]])
In [23]:# ver la dimension de una matriz
A.shape
Out[23]: (3, 3)
In [24]: # ver cantidad de elementos de una matriz
A.size
Out[24]:9

Multiplicación parte 2

In [25]:# Ejemplo multiplicación de matrices


A = np.arange(1, 13).reshape(3, 4) #matriz de dimension 3x4
A
Out[25]:array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12]])
In [26]:B = np.arange(8).reshape(4,2) #matriz de dimension 4x2
B
Out[26]:array([[0, 1],
[2, 3],
[4, 5],
[6, 7]])
In [27]:# Multiplicando A x B
A @ B #resulta en una matriz de dimension 3x2
Out[27]:array([[ 40, 50],
[ 88, 114],
[136, 178]])
Identidad transpuesta identidad
In [29]:# Creando una matriz identidad de 2x2
I = np.eye(2)
I
Out[29]: array([[1., 0.],
[0., 1.]])
In [30]:# Multiplicar una matriz por la identidad nos da la misma matriz
A = np.array([[4, 7],
[2, 6]])
A
Out[30]:array([[4, 7],
[2, 6]])
In [31]:A @ I # AxI = A
Out[31]:array([[4., 7.],
[2., 6.]])
In [32]:# Calculando el determinante de la matriz A
np.linalg.det(A)
Out[32]:10.000000000000002
In [33]:# Calculando la inversa de A.
A_inv = np.linalg.inv(A)
A_inv
Out[33]:array([[ 0.6, -0.7],
[-0.2, 0.4]])
In [34]:# A x A_inv nos da como resultado I.
A @ A_inv
Out[34]:array([[1., 0.],
[0., 1.]])
In [35]:# Trasponiendo una matriz
A = np.arange(6).reshape(3, 2)
A
Out[35]:array([[0, 1],
[2, 3],
[4, 5]])
In [36]:np.transpose(A)
Out[36]:array([[0, 2, 4],
[1, 3, 5]])

Das könnte Ihnen auch gefallen