如何从.dat文件中提取一堆2d矩阵并将它们放入一个大3d矩阵中? [英] How do I take a bunch of 2d matrices from .dat files and put them into one big 3d matrix?
问题描述
我正在尝试将.dat文件中的所有这些2d矩阵合并为一个3d矩阵.
I'm trying to combine all these 2d matrices I have in .dat files into a single 3d matrix.
所以到目前为止,我要做的是:
So what I've done so far is this:
for (i=1:40) //There are 40 of these files
fileName = ['Solutionm1.dat/Solution' num2str(i-1) '.dat'] //This line sets a file name
f=fopen(fileName,'r') //I'm just opening the file now
A{i}=fread(f,'double') //Now I'm converting the binary file into a matlab matrix
B{i}=reshape(A{i},41,21) //Now I'm putting it into the shape that I want (I don't know of a better way to read binary files)
fclose all
end
最终,我想通过使用norm((insert 3d matrix here),2)
我遇到的问题是我只是不知道如何将我刚刚制作的所有矩阵组合成一个大3D矩阵.
The problem I'm having is that I just don't know how to combine all the matrices I just made into one big 3D matrix.
解决方案
使用
T(:,:,i)=B{i}
或使用
T=cat(3,B{:})
继续存在的问题
这现在不起作用:
norm(T,2) //Should compute the L2 norm of my 3D matrix, right?
这可能超出了这个问题的范围...
This might be out of the scope of this question though...
据我了解,我认为必须在2D矩阵上使用规范.
From what I've learned, I think norm has to be used on a 2D matrix.
推荐答案
一旦有了B,请运行:
matrix3d = zeros(41,21,40);
for i=1:40
matrix3d(:, :, i)=B{i};
end
您还可以在循环中包含matrix3d(:, :, i)=B{i};
,并在进入循环之前调用matrix3d = zeros(41,21,40);
.
You can also include matrix3d(:, :, i)=B{i};
in your loop, and call matrix3d = zeros(41,21,40);
before entering the loop.
这篇关于如何从.dat文件中提取一堆2d矩阵并将它们放入一个大3d矩阵中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!