Transforming vectors into a Matrix

Asked

Viewed 1,540 times

1

I have several signals, where S1, s2 until Sn, are vectors of size n, I would like to unite them in a matrix to stay as follows:

matriz = ( [s1]
           [s2]
           ...
           [sn] )

So that I can access an element at any point, for example [4][433], the value of row position 4 and column 433.

I tried approaches with np.vstack(), but np.vstack() really does the proposed? Or I traveled?

  • 1

    This will be: https://ideone.com/ynwhzM

2 answers

3


There is no mystery:

matrix = [ [  1,  2,  3,  4,  5 ],
           [  6,  7,  8,  9, 10 ],
           [ 11, 12, 13, 14, 15 ],
           [ 16, 17, 18, 19, 20 ] ]

print( matrix[2][3] )

Alternatively, you can use the method append():

matrix = []

matrix.append( [  1,  2,  3,  4,  5 ] )
matrix.append( [  6,  7,  8,  9, 10 ] )
matrix.append( [ 11, 12, 13, 14, 15 ] )
matrix.append( [ 16, 17, 18, 19, 20 ] )

print( matrix[2][3] )
  • Thank you for the reply and cordiality.

1

Imagine you have multiple vectors SAME-LENGTH - vectors that have the same number of elements - and want to mount a "matriz" with these vectors. In this case, you can use the algorithm shown below. Also note that to mount said algorithm, I imported the library numpy, previously installed in my IDE.

import numpy as np

while True:
    try:
        m = int(input('Desejas manipular quantos vetores? '))
        if m < 1:
            print('\033[31mValor INVÁLIDO! Digite apenas valores maiores que "0"!\033[m')
        else:
            break
    except ValueError:
        print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')

while True:
    try:
        n = int(input('Quantos elementos terão em cada vetor? '))
        if n < 1:
            print('\033[31mValor INVÁLIDO! Digite apenas valores maiores que "0"!\033[m')
        else:
            break
    except ValueError:
        print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')

listaA = list()
for i in range(1, m + 1):
    vetor = list()
    for j in range(1, n + 1):
        while True:
            try:
                v = int(input(f'Digite o {j}º valor da {i}ª lista: '))
                break
            except ValueError:
                print('\033[31mValor INVÁLIDO! Digite apenas valores inteiros!\033[m')
        vetor.append(v)
    listaA.append(vetor)

matriz = np.array(listaA)
print(f'\033[32mA matriz gerada foi:\n{matriz}\033[m')

Note that when we run this algorithm the following message is displayed: Desejas manipular quantos vetores?. At this point we must type the amount of vectors and press enter. Then we received the second message; Quantos elementos terão em cada vetor?. At this point we must enter the amount of elements of each vector and press enter.

From this moment we must insert each element of each vector and then press enter.

Once having inserted all elements of all vectors, the algorithm will assemble a list of lists defined as listaA. Then the listaA is subjected to the method array library numpy, thus producing a tabular array where, each row corresponds to a vector.

Later this matrix will be displayed on the screen.

Browser other questions tagged

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