2011-03-18 101 views
3

比方說,我想創建一個矩陣A尺寸爲3×4×4與單個語句(一個平等,沒有任何串聯),像這樣:聲明在一個聲明多維數組

%// This is one continuous row 
A = [ [ [3 3 4 4], [8 11 8 7], [4 4 6 7], [4 7 6 6] ]; ... 
     [ [3 2 4 2], [9 6 4 12], [4 3 3 4], [5 10 7 3] ]; ... 
     [ [2 2 1 2], [3 3 3 2], [2 2 2 2], [3 3 3 3] ] ] 

回答

6

concatenation operator []只能在2個維度上工作,如水平連接的或垂直連接的[a; b]。要創建matrices with higher dimensions,您可以使用reshape函數,或初始化所需大小的矩陣,然後用您的值填充它。例如,你可以這樣做:

A = reshape([...], [3 4 4]); % Where "..." is what you have above 

或者這樣:

A = zeros(3, 4, 4); % Preallocate the matrix 
A(:) = [...];  % Where "..." is what you have above 
+0

第二個就是我所做的。非常感謝你 – Viktor 2011-03-18 17:47:07

6

您可以使用cat沿着第三維 「層」 2-d矩陣,例如:

A = cat(3, ones(4), 2*ones(4), 3*ones(4)); 

從技術上講,這是連接,但它仍然只有一個任務。

CATLAB