0

I import a .txt file with the command tab = np.genfromtxt() and it creates a kind of matrix. I need to work with its contents. When I visualize the elements with the command

for i in range n:
    print(tab[n][:])

it works and I can use matrix elements with [][] like if the first is the line number and the second the column one. Instead when I use the command tab[0][:] the command is like I used tab[:][0], i.e. I can visualize only the line 0 with all its elements (tab[0][:]=tab[:][0]). What's the way I can move in a fixed column number and use all the line elements without a cicle "for i in range()" type? Thank you.

Azad
  • 1,050
  • 8
  • 24
Raizen
  • 99
  • 2
  • 10

1 Answers1

1

All indices of a numpy array should be in a single [] separated by commas (ie tab[i,j]), if you provide just one index you get an array with the first dimension equal to that index (a row in this case). So tab[:] returns the whole matrix again. When you apply [0] over tab[:] it's the same as tab[0]

For iterating over columns:

for i in range n:
    print(tab[:,n])

For iterating over rows:

for i in range n:
    print(tab[n,:])
Azad
  • 1,050
  • 8
  • 24