What I am basically trying to do is convert from some MATLAB code to Python:
The MATLAB code:
for time = 100:model_times
for i = 1:5
indat = fread(fid,[48,40],'real*4');
vort(:,:,i,time) = indat';
end
end
fid holds the file path (a DAT file) is being used. vort is a preallocated as: vort = zeros(40,48,5,model_times). model_times is a fixed integer (e.g. 100).
What seems to be happening is that the .dat file data is being read in as a 48x40 matrix, then inserted into the preallocated array vort, at a fixed i and time (the loop counters).
I have attempted this in Python:
for time in range(model_times):
for i in range(5):
vort[:,:,i,time] = np.fromfile(fid,np.float64)
I receive an error that says, "ValueError: operands could not be broadcast together with shapes (40,48) (6048000)". The error occurs on the last line of Python code above. I have also tried adding .reshape((40,48,5,model_times)) to the line with the error, but receive another error that says "ValueError: total size of new array must be unchanged."
So my question is, what is the Python equivalent to MATLAB's "fread", that can handle multidimensional arrays?
On a scale from 1 to 10, with 1 being a total beginner and 10 being a seasoned veteran, I'm about a 4.
解决方案
This should work too. No reason you can't do it all in a single read:
vort = np.fromfile(fid, np.float64).reshape((model_times,5,48,40)).transpose()
You have to be careful to reshape the 1-D array into the native order of the array indices in the file (model_times,5,48,40), then use transpose to reorder the indices to what you want (40,48,5,model_times). If you tried to reshape directly to the latter, you'd get data in the wrong places.
本文讨论了从MATLAB代码转换到Python过程中遇到的问题,特别是如何使用Python等效于MATLAB的fread函数来处理多维数组的读取,并提供了一个有效的解决方案。
577

被折叠的 条评论
为什么被折叠?



