1

我正在尝试将 .dat 文件中的所有这些 2d 矩阵组合成一个 3d 矩阵。

所以到目前为止我所做的是:

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

最终,我想通过使用这个 3d 矩阵的 L2 范数norm((insert 3d matrix here),2)

我遇到的问题是我只是不知道如何将我刚刚制作的所有矩阵组合成一个大的 3D 矩阵。

解决方案

利用

T(:,:,i)=B{i}

或使用

T=cat(3,B{:})

持续的问题

这现在不起作用:

norm(T,2) //Should compute the L2 norm of my 3D matrix, right?

虽然这可能超出了这个问题的范围......

根据我所学到的,我认为 norm 必须用于 2D 矩阵。

4

2 回答 2

1

这就是答案!

T=Cat(3,B{:}) //Combines all matrices into one single 3D matrix
于 2013-05-17T03:08:12.983 回答
0

一旦你有了 B,运行:

matrix3d = zeros(41,21,40);
for i=1:40
    matrix3d(:, :, i)=B{i};
end

您还可以在循环中包含,并在进入循环之前matrix3d(:, :, i)=B{i};调用。matrix3d = zeros(41,21,40);

于 2013-05-17T03:05:21.657 回答