在 Matlab 中将未格式化的数据读入 4D 数组(与 IDL)
我有一个未格式化(我认为是二进制)数据的 .img 文件,我想将其作为浮点读入 Matlab 中的 4D 数组中。我注意到Matlab中用于读取二进制数据的“fread”函数只能自动读入一维或二维数组,所以我尝试了这样的操作:
function fileArr4D= load4DFile(dim1, dim2, dim3, dim4, filename)
fileArr4D= zeros(dim1, dim2, dim3, dim4); %initialize array
fid= fopen(strcat('files/', filename));
for l= 1:dim4
for k= 1:dim3
fileArr4D(:, :, k, dim4)= fread(fid, [dim1, dim2], 'float');
end
end
fclose(fid);
fileArr4D= flipdim(fileArr4D, 2); %flips along dimension 2 (of 4)
end
最后一行是翻转数据(特定于数据的东西),并且我将其包含在内的唯一原因是以防万一它是错误的根源。
不管怎样,这个函数给了我一个 4D 数组,但不是我想要的值。我实际上正在将一些代码从IDL翻译到Matlab,在IDL中,实现是:
pp=fltarr(dim1,dim2,dim3,dim4)
file='pathto/filename.img'
openr,1,file
readu,1,pp
pp=reverse(pp,2)
close,1
但是,将Matlab中fileArr4D返回的数组与IDL中的pp进行比较,数组的最小值、最大值和平均值都不同。关于这两种情况的不同做法,或者在 Matlab 中将二进制数据读入 4D 数组的更自然的方式,您有什么想法吗?
顺便说一句,如果我切换 Matlab 函数中的循环顺序(for k= 1:dim3, for l= 1:dim4, ...
而不是 for l= 1:dim4 ,对于 k= 1:dim3, ...
),我得到了正确的最小值和最大值,但平均值错误(与之前的平均值相同)。
任何将 IDL 片段的行为引入 Matlab 的帮助都将不胜感激。
I have a .img file of unformatted (I think binary) data that I want to read as floats into a 4D array in Matlab. I noticed that the 'fread' function in Matlab used to read binary data can only read into 1D or 2D arrays automatically, so I tried something like this:
function fileArr4D= load4DFile(dim1, dim2, dim3, dim4, filename)
fileArr4D= zeros(dim1, dim2, dim3, dim4); %initialize array
fid= fopen(strcat('files/', filename));
for l= 1:dim4
for k= 1:dim3
fileArr4D(:, :, k, dim4)= fread(fid, [dim1, dim2], 'float');
end
end
fclose(fid);
fileArr4D= flipdim(fileArr4D, 2); %flips along dimension 2 (of 4)
end
The last line is to flip the data (something specific to the data), and the only reason I included it is in case it is the source of a bug.
Anyways, this function gives me a 4D array, but not with the values I want. I am actually translating some code from IDL to Matlab, and in IDL, the implementation is:
pp=fltarr(dim1,dim2,dim3,dim4)
file='pathto/filename.img'
openr,1,file
readu,1,pp
pp=reverse(pp,2)
close,1
However, comparing the array returned by fileArr4D in Matlab with pp in IDL, the min, max, and mean of the arrays are all different. Any ideas as to what is being done differently in the two cases, or of a more natural way to read binary data into a 4D array in Matlab?
Btw, if I switch the order of looping in the Matlab function (for k= 1:dim3, for l= 1:dim4, ...
instead of for l= 1:dim4, for k= 1:dim3, ...
), I get the correct min and max but wrong mean (which is the same mean as before).
Any help in getting the behavior of the IDL snippet into Matlab would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您想使用
fread
来访问数据,并且它只能读取一维或二维数组,因此我建议将数据作为一维数组加载。一旦您获得一维数组的数据,只要您知道维度应该是什么,您就可以对一维数组调用reshape
函数,将其重新格式化为适当的 4D 数组尺寸。Since you want to use
fread
to access the data and it can only read 1D or 2D arrays, I'd suggest loading the data in as a 1D array. Once you have the data as a 1D array, as long as you know what the dimensions are supposed to be, you could call thereshape
function on the 1D array to reformat it as a 4D array of the appropriate size.