Multiply matrices in python

2

This is a question a bit simple but I can not solve it on my own, and I have not found any post related to this topic, my question is this:

matriz=[[1,1,1],[1,1,1],[1,1,1]]
print(matriz)

matriz[0]=matriz[0]*2

print(matriz)

What I want to do is multiply by two each grid in row 0, so that it looks like this:

matriz=[[2,2,2],[1,1,1],[1,1,1]]

And not like this:

matriz=[[1, 1, 1, 1, 1, 1], [1, 1, 1], [1, 1, 1]]

It would not be inconvenient for me to use numpy, but I have not been able to solve the problem with numpy either.

Greetings, thank you.

    
asked by Nexobeta28 YT 21.05.2018 в 22:37
source

3 answers

2

With numpy it is trivial, in fact it works as you have written it. But yes, your matrix must be created with numpy.array , instead of with python lists. So:

import numpy as np
matriz= np.array([[1,1,1],[1,1,1],[1,1,1]])
print(matriz)

matriz[0]=matriz[0]*2

print("---------------")
print(matriz)

Exit:

[[1 1 1]
 [1 1 1]
 [1 1 1]]
---------------
[[2 2 2]
 [1 1 1]
 [1 1 1]]
    
answered by 21.05.2018 / 22:53
source
1

Basically what you're doing is this:

print([1,1,1]*2)
[1, 1, 1, 1, 1, 1]

[1,1,1] is a common Python list, the __mul__ method or specifically the * when you do [1,1,1]*2 is not an arithmetic multiplication, according to documentation for data type Sequence (a list is):

  

s * n or n * s equivalent to adding itself to times

In other words, do: [1,1,1]*2 equals replicating [1,1,1] twice. The way to achieve multiplication, from a list of lists of python (matrix) would be to do it through an understanding, as follows:

matriz=[[1,1,1],[1,1,1],[1,1,1]]

matriz[0] = [a*2 for a in matriz[0]]
print(matriz)
[[2, 2, 2], [1, 1, 1], [1, 1, 1]]

But if in fact, you are going to use numpy the response of abulafia is the appropriate one.

    
answered by 21.05.2018 в 23:04
1
___ erkimt ___ Multiply matrices in python ______ qstntxt ___

This is a question a bit simple but I can not solve it on my own, and I have not found any post related to this topic, my question is this:

import numpy as np

matriz = np.array([[1,1,1],
                   [1,1,1],
                   [1,1,1]])

matriz[0] *= 2

What I want to do is multiply by two each grid in row 0, so that it looks like this:

>>> matriz
array([[2, 2, 2],
       [1, 1, 1],
       [1, 1, 1]])

And not like this:

matriz = [[1,1,1],
          [1,1,1],
          [1,1,1]]


for i in range(len(matriz[0])):
    matriz[0][i] *= 2

It would not be inconvenient for me to use numpy, but I have not been able to solve the problem with numpy either.

Greetings, thank you.

    
______ azszpr166443 ___

With numpy it is trivial, in fact it works as you have written it. But yes, your matrix must be created with *= , instead of with python lists. So:

matriz = [[1,1,1],
          [1,1,1],
          [1,1,1]]

matriz[0] = [n * 2 for n in matriz[0]]

Exit:

import numpy as np

matriz = np.array([[1,1,1],
                   [1,1,1],
                   [1,1,1]])

matriz[0] *= 2
    
______ azszpr166449 ___

Basically what you're doing is this:

>>> matriz
array([[2, 2, 2],
       [1, 1, 1],
       [1, 1, 1]])

for is a common Python list, the %code% method or specifically the %code% when you do %code% is not an arithmetic multiplication, according to documentation for data type Sequence (a list is):

  

s * n or n * s equivalent to adding itself to times

In other words, do: %code% equals replicating %code% twice. The way to achieve multiplication, from a list of lists of python (matrix) would be to do it through an understanding, as follows:

matriz = [[1,1,1],
          [1,1,1],
          [1,1,1]]


for i in range(len(matriz[0])):
    matriz[0][i] *= 2

But if in fact, you are going to use %code% the response of abulafia is the appropriate one.

    
______ ___ azszpr166450

Numpy allows this currently using the assignment operator %code% , performing the operation directly on the array elements:

matriz = [[1,1,1],
          [1,1,1],
          [1,1,1]]

matriz[0] = [n * 2 for n in matriz[0]]
%pre%

With a Python list you have several options but it is more cumbersome and less efficient, for example:

  • Use a %code% to go through the list and do the in-place operation:

    %pre%
  • Using list compression:

    %pre%
___
answered by 21.05.2018 в 23:04