在单个语句中声明多维数组
假设我想用一条语句创建一个维度为 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] ] ]
Let's say I want to create a matrix A
with dimensions 3×4×4 with a single statement (i.e one equality, without any concatenations), something like this:
%// 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] ] ]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
cat
来“分层”例如,沿第三维的二维矩阵:从技术上讲,这是串联,但它仍然只是一个赋值。
You can use
cat
to "layer" 2-D matrices along the third dimension, for example:Technically this is concatenation, but it's still only one assignment.
连接运算符
[]
仅适用于二维,例如[ab]
水平连接或[a; b]
垂直连接。要创建更高维度的矩阵,您可以使用reshape
函数,或初始化矩阵您想要的大小,然后用您的值填充它。例如,您可以这样做:或者这样:
The concatenation operator
[]
will only work in 2 dimensions, like[a b]
to concatenate horizontally or[a; b]
to concatenate vertically. To create matrices with higher dimensions you can use thereshape
function, or initialize a matrix of the size you want and then fill it with your values. For example, you could do this:Or this: