2013-05-17 60 views
1

我試圖將所有這些2d矩陣我在.dat文件中合併成一個3d矩陣。如何從.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 

最後,我想用norm((insert 3d matrix here),2)

我遇到的問題是,藉此3D矩陣的L2規範我只是不知道如何將我剛剛製作的所有矩陣組合成一個大的3D矩陣。

解決方案

使用

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

或使用

T=cat(3,B{:}) 

續問題

這並不是現在的工作:

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

這可能是出了這個問題,雖然範圍的...

據我瞭解,我認爲規範必須建立在一個二維矩陣使用。

回答

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);

+0

啊!這也適用!接受答案! – Mechy

+0

嗯,標準(T,2)不起作用的原因是什麼? – Mechy

+0

對不起,我頭上沒有答案,現在需要去開會。 –

1

下面是答案!

T=Cat(3,B{:}) //Combines all matrices into one single 3D matrix 
+0

噢,真好!但可能更適合直接在循環中填充3D,而不必創建臨時變量B. –

+0

是的,我明白了你的觀點。我認爲 – Mechy

相關問題