假设我想创建一个尺寸为3×4×4的矩阵A
,其中包含一个语句(即一个相等,没有任何连接),如下所示:
%// 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] ] ]
答案 0 :(得分:6)
concatenation operator []
仅适用于2个维度,例如[a b]
可以水平连接,[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
答案 1 :(得分:6)