2013-02-26 168 views
1

我對Matlab很陌生,正在嘗試填充3維數組。基本上我有4個批次的1x81矩陣,我想在一個4x1x81矩陣中。我試圖使用for循環將每個1x81拼接成4x1x81,但迄今爲止還沒有任何運氣。我確信有一種更簡單的方法,但需要一雙新鮮的眼睛。任何幫助將不勝感激,謝謝!Matlab填充3維數組。

回答

0

我會給你一個例子,然後你應用相同的技術。您必須使用colon:運營商來完成此任務。

a=1;b=2;c=3; 

然後,3 d矩陣可以形成爲:

new3D_Mat(:,:,1)=a; 
new3D_Mat(:,:,2)=b; 
new3D_Mat(:,:,3)=c; 

Output: 

>> new3D_Mat 

new3D_Mat(:,:,1) = 

    1 

new3D_Mat(:,:,2) = 

    2 

new3D_Mat(:,:,3) = 

    3 
0

正是你嘗試什麼? matrix(1,1,:)=myvector;matrix(2,1,:)=anotherone;或者你用循環變量替換第一個索引的循環呢?考慮

>> m(1,1,:)=rand(1,3) 
m = 
(:,:,1) = 
    0.3478 
(:,:,2) = 
    0.0276 
(:,:,3) = 
    0.5313 

出於好奇,4x81矩陣出了什麼問題? (如果您已經有一個,permute可能會幫助您獲得4x1x81 3d陣列。)

0

爲什麼使用4x1x81矩陣? 看看從四個1x81矩陣中創建4x81矩陣是多麼容易。

% Matrix of all ones 
a = ones(1, 81); 

% Matrix of all twos 
b = ones(1, 81); 
b = b .*2; 

% Matrix of all threes 
c = ones(1, 81); c = c .*3; 

% Matrix of all fours 
d = ones(1, 81); d = d .*4; 

% Aggregate 
all_of_em = [a; b; c; d]; 

運行whos查看您的變量。

Name   Size   Bytes Class  Attributes 

a    1x81    648 double    
all_of_em  4x81    2592 double    
b    1x81    648 double    
c    1x81    648 double    
d    1x81    648 double