Q:

Rehaspe a 2D matrix into a 3D ? (x, y) -> (x/72,72,y)

I have a text file from which I load the original matrix.
The text file has comments with # and it basically has multiple matrices of 77*44.
I would like to read this file and store each matrix from this complete number of mats.
import os
import sys
import numpy as np
from numpy import zeros, newaxis
import io

#read the txt file and store all vaules into a np.array
f = open('/path/to/akiyo_cif.txt','r')
x = np.loadtxt(f,dtype=np.uint8,comments='#',delimiter='\t')

nof = x.shape[0]/72
print ("shape after reading the file is "+ str(x.shape) )

#example program that works
newmat =np.zeros((nof+1,72,44))
for i in range(0,nof+1):
    newmat[i,:,:]= x[i*72 : (i*72)+72 , :]
print ("Shape after resizing the file is "+ str(newmat.shape) )

Output :-Shape after reading the file is (21240, 44)
Shape after resizing the file is (274, 72, 44)

If I run this 
newmat=x.reshape((nof,72,44))

newmat = x.reshape((nof,72,44))
ValueError: total size of new array must be unchanged

I would like to re size this matrix to (21240/72,72,44). 
Where the first 77 lines corresponds to newmat[0,:,:] and the next 77 lines to newmat[1,:,:].

A:

Use x.reshape(-1, 72, 44):
In [146]: x = np.loadtxt('data' ,dtype=np.uint8, comments='#', delimiter='\t')

In [147]: x = x.reshape(-1, 72, 44)

In [148]: x.shape
Out[148]: (34, 72, 44)

When you specify one of the dimensions as -1, np.reshape replaces the -1 with a value inferred from the length of the array and the remaining dimensions.

