元胞数组语法
两个问题:
1)我发现一段代码,上面写着 cellArr{x}{y}{3,8} = 1.0;
,我想知道 {3 ,8}
表示。该程序将连接图中的各个节点连接在一起。这里我们会说“在图 x 的集合中,图 y 从 3 到 8 的连接的顶点标签为 1.0”。不过,总的来说,语法 {3,8}
在 MatLab 中意味着什么?
2)这可能不是这个问题的地方,但如果我知道我总是会有顶点值,即小数/浮点数,我真的应该使用元胞数组吗?因为我知道我只会有一种数据类型,所以矩阵会更好吗?
谢谢 :)。
Two questions:
1) I've found a piece of code that says something like cellArr{x}{y}{3,8} = 1.0;
, I was wondering what the {3,8}
means. The program connections various nodes in a collections of connected graphs together. Here we would say that "in the set of graphs x, the graph y's connection from 3 to 8 has a vertex label of 1.0". Still, in general what does the syntax {3,8}
mean in MatLab?
2) This probably isn't the place for this question but should I really be using cell arrays if I know I'm always going to be having vertex values i.e. decimals/floats. Would a matrix be better because I know I'm only going to have a single data type?
Thank you :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
元胞数组可以有多个维度,因此它们可以像其他索引一样使用多个下标进行索引 多维数组。语法
{3,8}
正在索引一个(可能是)二维元胞数组,获取第三行第八列中的元胞内容。使用元胞数组有两个主要原因:存储不同类型的数据或存储不同大小的数据。假设示例中的 x 和 y 是标量索引,则 cellArr 是一个元胞数组,其中元胞由 x 索引> 包含另一个元胞数组,其由
y
索引的元胞包含一个存储顶点标签的二维元胞数组。现在,如果您的顶点标签都是相同的数据类型并且都只是单个非空(即不是
[]
)值,那么最低级别的二维元胞数组可以转换为二维数值数组,您的索引将如下所示:现在的问题是如何处理由
x
和y
索引的两组封闭元胞数组。如果可以通过 y 索引的每个单元格都包含大小和类型相同的二维数值数组,则该单元格数组可以转换为 3D 数值数组可以像这样索引的数组:最后,如果可以通过
x
索引的每个单元格都包含同样大小和类型相同的 3-D 数值数组,则cellArr
可以转换为 4-D 数值数组,可以像这样进行索引:您可以根据自己的喜好更改下标的顺序(即
numArr
的尺寸),但我将x
和y
放在因此,如果您要索引顶点标签的子数组,例如 numArr(:,:,y,x) ,它将以二维数组的形式返回。如果您对索引进行了排序,以便对顶点标签的子数组进行索引,例如 numArr(x,y,:,:),它将以 4 维数组的形式返回结果,其中包含两个主要的单例维度(您必须使用 SQUEEZE 等函数删除它们)。Cell arrays can have multiple dimensions, and thus they can be indexed with multiple subscripts like any other multidimensional array. The syntax
{3,8}
is indexing a (presumably) 2-D cell array, getting the contents of the cell in the third row and eighth column.There are two main reasons to use cell arrays: storing data of different types or storing data of different sizes. Assuming
x
andy
are scalar indices in your example, thencellArr
is a cell array with the cell indexed byx
containing another cell array, whose cell indexed byy
contains a 2-D cell array which stores your vertex labels.Now, if your vertex labels are all the same data type and are all just single non-empty (i.e. not
[]
) values, then the 2-D cell array at the lowest level could be turned into a 2-D numeric array, and your indexing will look like this:The question now becomes how to handle the two enclosing sets of cell arrays indexed by
x
andy
. If every cell that can be indexed byy
contains 2-D numeric arrays all of the same size and type, then that cell array could be turned into a 3-D numeric array that could be indexed like so:Finally, if every cell that can be indexed by
x
contains 3-D numeric arrays that are again all of the same size and type, thencellArr
could be turned into a 4-D numeric array that could be indexed like so:You could change the order of the subscripts (i.e. the dimensions of
numArr
) to your liking, but I putx
andy
at the end so that if you were to index a subarray of vertex labels likenumArr(:,:,y,x)
it will return it as a 2-D array. If you had the indices ordered such that you would index a subarray of vertex labels likenumArr(x,y,:,:)
, it will return the result as a 4-D array with ones for the two leading singleton dimensions (which you would have to remove using functions like SQUEEZE).{3,8}
是元胞数组索引,就像{x}
和{y}
一样。所以 cellArr 是细胞向量的细胞向量。这些单元向量之一由{x}
索引。该元胞向量本身就是一个元胞二维矩阵向量,由{y}
索引。最后,这个单元矩阵由{3,8}
索引,即第3行第8列。numericArray[x, y, 3, 8]
索引。{3,8}
are cell array indices just as the{x}
and{y}
are. So cellArr is a cell vector of cell vectors. One of these cell vectors is indexed by{x}
. This cell vector is itself a vector of cell 2d-matrices which is indexed by{y}
. Finally, this cell matrix is indexed by{3,8}
, i.e. the 3rd row and 8th column.numericArray[x, y, 3, 8]
.