3

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 :).

2 Answers 2

4
  1. 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.

  2. There are two main reasons to use cell arrays: storing data of different types or storing data of different sizes. Assuming x and y are scalar indices in your example, then cellArr is a cell array with the cell indexed by x containing another cell array, whose cell indexed by y 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 and y. If every cell that can be indexed by y 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, then cellArr 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 put x and y at the end so that if you were to index a subarray of vertex labels like numArr(:,:,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 like numArr(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).

Sign up to request clarification or add additional context in comments.

2 Comments

"with EACH cell storing another cell array ... with EACH cell storing another cell array ... with EACH cell of that storing a 2-D cell array" - I believe this is incorrect, since cell array can store any data type and size in any cell. In this particular case we can say that cell indexed by scalar numeric 'x' in 'cellArr' contains another cell array, etc.
@yuk: True, I was generalizing based on the assumption that, for the specific problem the OP describes, each cell array probably stores the same type of data. However, I'll refine the text as you suggest.
4
  1. 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.
  2. 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].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.