Matrix NxM (2D) in Python with Numpy

0

I'm trying to solve a college activity but I could not transpose the logic into the code. The case is as follows: "The user will enter the integers in the same row separated by space. After the user passes the values, the following values will be used as data from the array.

Ex:

2 2
3 4
5 6

array will be ([3, 4], [5, 6])

that is, a 2 x 2 matrix.

    
asked by anonymous 16.08.2018 / 01:52

5 answers

2
  • You will need to read the user input in order to identify the size of the array. Do this using the input ;

    a. The return of the input function will always be a string , so you need to convert your string to a sequence of two integers. Do this with the help of string.split and int ;

  • Possessing the dimensions of the array, NxM, you will have to read N times the user input, which will be the rows of the array. Again use the input function to read the entry and repeat the process with a loop repeat. I recommend doing the for structure supported by the range ;

    a. Again, remember that the return of input will always be a string , so use the same logic as 1.a to convert it to a sequence of numbers;

  • Tips:

  • An array can be represented as a list of lists;
  • You can initialize an empty list as lista = [] ;
  • You can add new elements to a list with lista.append(...) ;
  • You can access a certain index in the list with lista[i] ;
  • Since you tried to do this, I'll put an example code:

    import numpy
    
    dimensions = input('Dimensões da matriz NxM: ').split()
    N, M = [int(value) for value in dimensions]
    
    matrix = []
    
    for i in range(N):
        row = input(f'Linha {i+1}: ').split()
    
        if len(row) != M:
            raise Exception(f'Você precisa informar {M} valores por linha')
    
        numbers = [int(number) for number in row]
        matrix.append(numbers)
    
    matrix = numpy.matrix(matrix)
    
    print(matrix)
    

    See working at Repl.it | GitHub GIST

    An example of the generated output is:

    >>> Dimensões da matriz NxM:  2 2
    >>> Linha 1:  1 2
    >>> Linha 2:  3 4
    [[1 2]
     [3 4]]
    
        
    16.08.2018 / 03:01
    0

    I've been able to do this, but it's not using the list pattern I want, I need to use numpy's features.

    import numpy as np
    lista = []
    matriz = input("Informe as dimensões da Matrix: ").strip().split()
    N = int(matriz[0])
    M = int(matriz[1])
    for i in range(N-1):
        for j in range(M):
            a = input().strip().split()
            a1 = int(a[0])
            a2 = int(a[1])
            lista.append(a1)
            lista.append(a2)
    print(lista)
    
        
    16.08.2018 / 03:20
    0

    Good afternoon @Anderson!

    Now in the afternoon, I made the code after reading yours. Following my own logic, to learn more. The result of the code was as follows:

    import numpy as np
    dimensoes = input().strip().split()
    N,M = (int(value) for value in dimensoes)
    matriz = []
    for i in range(N):
        a = input().strip().split()
        if i < M:
            for j in range(M):
                a1 = a[j]
                a1 = (int(a1))
                matriz.append(a1)
        else:
            print("ERROR")   
    matriz = np.array(matriz)
    print(matriz)
    print(np.transpose(matriz))
    print(matriz.flatten())
    

    The problem now is that the transpose () and flatten () methods are not working as they should.

        
    16.08.2018 / 22:52
    0

    So, if other people have the same problem, I am putting the solution I created for my own problem that 100% of my case answers.

    import numpy as np
    dimensoes = input().strip().split()
    N,M = (int(value) for value in dimensoes)
    matriz = []
    for i in range(N):
        if i < M:
            a = input().strip().split()
            matriz.append(list(map(int, a)))
        else:
            print("ERROR")
    matriz = np.array(matriz)
    print(matriz)
    print(matriz.transpose())
    print(matriz.flatten())
    
        
    17.08.2018 / 16:43
    0

    Following new code with treatments:

    import numpy as np
    import sys
    def mat(a):
        a1 = a
        a1 = list(map(int, a1))
        matriz.append(a1)
        return matriz
    
    dimensoes = input().strip().split()
    N,M = (int(value) for value in dimensoes)
    matriz = []
    saida = 0
    for i in range(N):
        if saida == 1:
            break
        a = input().strip().split()
        if len(a) > M:
            print("Tamanho de N informado é maior que Dimensão inicial de N!")
            saida = 1
            break
        elif len(a) > N:
            print("Tamanho de M informado é maior que Dimensão inicial de M!")
            saida = 1
            break
        else:
            mat(a)
    if saida == 0:
        matriz = np.array(matriz)
        print(np.transpose(matriz))
        print(matriz.flatten())
    
        
    17.08.2018 / 19:16