Eliminate rows and columns of a matrix, taking into account its elements

1

I have to create a program that eliminates rows and columns

the program has to analyze all the elements of a column and if all are 0, that column and its corresponding row is deleted

for example if all the elements in column 6 are 0, column 6 and row 6 are deleted

first the program asks the user for the dimensions of the matrix and then its values

import numpy
from numpy import delete
m = int(input("Dimension de la matriz:")) 
matriz_v = numpy.zeros((m,m)) 
print ("Introducir los elementos de la matriz") 
for f in range(0,m):
    for c in range(0,m):
        matriz_v[(f),(c)] = (input("Elemento ["+str(f+1)+","+str(c+1)+"] "))
print (matriz_v)    

For example, all the elements in column 4,5,6 are 0

and I want this where the rows and columns 4,5,6 are deleted

I am trying with this idea, add all the elements of the column and if the sum is equal to 0, this is eliminated, (since in my case of study the only way that the sum of all the terms of a column is 0, is if all the terms are 0)

for x in range(0,m): 
    suma_columna = 0
    for y in range(0,m):
        suma_columna = suma_columna +  matriz_v[y][x]
        if (suma_columna == 0):
            matriz_v_c = delete(matriz_v, [x], axis=1)
            matriz_v_c_f = delete(matriz_v_c, [x], axis=0)

but I only delete 1 row and 1 column and not all that I need

thanks

    
asked by Luis Hugo Barrera Luna 19.05.2017 в 21:10
source

1 answer

1

The problem is that matriz_v_c and matriz_v_c_f are reddefinen for each cycle of the first for, this causes that in the end only the result of the last iteration is applied.

It can be done in this way but it is complicating life unnecessarily, so that NumPy and Python do 'magic':

  • First we look for a way to create an array of boleanos in which each element will be True if that column is good (it has some element other than 0) and False otherwise. In your example, the array would be:

    [ True  True  True  True False False False]
    
  • Then we apply slicing on the original matrix using that array of voleanos. Yes, it turns out that Numpy allows you to do things like:

    >>> import numpy as np
    >>> m = np.array([[1,2,3],
                     [4,5,6]])
    >>> filtro = np.array([True, False, True])
    >>> m[:, filtro]
    array([[1, 3],
           [4, 6]])
    

In your case the code would be simply:

good_cols = numpy.any(matriz_v.T != 0,  axis = 1)
matriz_v = matriz_v[:, good_cols][good_cols, :]

numpy.any is used that returns True as soon as some element of the column is not 0 (Short-circuit evaluation), which increases efficiency on sum or all .

In case you want to reproduce the code with your example:

matriz = [[0.003,     0,     0,     -1,     0,    0,    0], 
          [    0, 0.004,     0,     -1,     0,    0,    0],
          [    0,     0,     0.1,   -1,     0,    0,    0],
          [    1,     1,     1,      0,     0,    0,    0], 
          [    0,     0,     0,      0,     0,    0,    0],
          [    0,     0,     0,      0,     0,    0,    0],
          [    0,     0,     0,      1,     0,    0,    0]]



matriz_v = numpy.array(matriz)
good_cols = numpy.any(matriz_v.T != 0,  axis = 1)
matriz_v = matriz_v[:, good_cols][good_cols, :]

Exit:

[[ 0.003  0.     0.    -1.   ]
 [ 0.     0.004  0.    -1.   ]
 [ 0.     0.     0.1   -1.   ]
 [ 1.     1.     1.     0.   ]]
    
answered by 20.05.2017 / 00:20
source