Cell Array Syntax
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 vert开发者_Python百科ex 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 :).
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:cellArr{x}{y}(3,8) = 1.0; %# Note the use of () instead of {}
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:cellArr{x}(3,8,y) = 1.0; %# Here I've chosen to use y as the third dimension
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:numArr(3,8,y,x) = 1.0;
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).
- The syntax
{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. - If all of your data is numeric then you'd be far better off using a 4 dimensional array. For your example, this array would be indexed by
numericArray[x, y, 3, 8]
.
精彩评论