5

I read a 4D array from a file which is given in a 2D form i, j, k, x, y, z. Input file header and shape I use numpy.reshape to reshape the 2D array to it's 3-D form. After making changes to this, I wish to write the file exactly the same order / format as I read it. I do not understand how to "reverse" numpy.reshape to put it back in the same format.

import numpy as np    
import pandas as pd
from pandas import read_csv 


header = read_csv("Input/Grid1_test.csv", nrows=1,skipinitialspace=True)

print header.head()

imax=header['IMAX'].iloc[0]
jmax=header['JMAX'].iloc[0]
kmax=header['KMAX '].iloc[0]

print(imax,jmax,kmax)

#read grid data with numpy array .. it is slow but probably worth it 
grid_data = np.loadtxt('Input/Grid1_test.csv', delimiter=',', skiprows=3)


size_arr = np.array(grid_data).reshape(kmax, jmax, imax, 6)

#do something

#write the grid back into the file

1 Answer 1

7

To "reverse" a reshape, you can just call reshape again on the array to reshape it into the original dimensions.

If you have an array x with dimensions (n, m) then:

x.reshape(kmax, jmax, imax, 6).reshape(n, m) == x
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Henry, that works! I should have thought of it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.