How to add more values in a numpy matrix?

Asked

Viewed 30 times

0

I am doing a program in python that uses matrices, I am using numpy to do them, but it is giving the following problem: I do the matrix in order to save several values, but it keeps only one. I was first making a list and dps converting to array using np.array(), but I wanted to do it another way, could I? Follow below my program:

import numpy as np                          #
import math                                 #               importando bibliotecas para criação das matrizes e cálculos com 
                                            #               números reais, respectivamente.

n1=n2=1                                     #               definindo constantes                                    
pi = math.pi                                #
M = 200 
                                            #
gama1 = 2/3                                                        #
b1 = math.sqrt(gama1) / (1+math.sqrt(1-gama1))                     #

gama2 = 2/3                                                        #   definindo os valores de b1 e b2                                               #
b2 = math.sqrt(gama2) / (1+math.sqrt(1-gama2))                     #

for l in range(1,M+1):
  for c in range(1,n1+1):
    w1 = np.array([b1*math.sqrt(1/(pi*(M+1)))*math.sin((l*c*pi)/(M+1))])  # adicionando valor na lista
  for c in range(1,n2+1):
    w2 = np.array([b2*math.sqrt(1/(pi*(M+1)))*math.sin((l*(n1+c)*pi)/(M+1))])  # adicionando valor na lista

in case the matrices are w1 and w2, and instead of appearing a 1x200 matrix, shows a 1x1 matrix

  • is pq at each loop you set the variable again. Then at the end you get only one list. You can use np.append to join the list to each loop.

  • I didn’t understand how to use this function, I used it in the program, but it didn’t work. I could give an example?

  • I answered. But please note the amount of information in your question and in your code that does not relate to the question. Try to leave in the question only the essential. See a guide here: https://pt.meta.stackoverflow.com/questions/1186/como-crea-um-exemplo-m%C3%adnimo-completo-e-verific%C3%a1vel

1 answer

2


The problem with your code is that every time the loop passes, it creates the line again. That’s why you only get one line at the end.

An array is basically a list of lists, so you can use the method append to join each row in each loop iteration. Example:

import numpy as np

linha1=[1,0,0]
linha2=[0,1,0]
linha3=[0,0,1]
linhas=[linha1,linha2, linha3]
matriz=[]

for linha in linhas:
    matriz.append(linha)

The variable matriz here is already a matrix, but to have access to mathematical methods for matrix transformation and analysis, I recommend using np.matrix numpy:

M=np.matrix(matriz)

print(M)
[[1 0 0]
 [0 1 0]
 [0 0 1]]

For example, you can invert the matrix using:

M.I

Which, in this case, returns the identity matrix itself, as we know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.