How do I create nested lists?

206 views (last 30 days)
Aaron Sartin
Aaron Sartin on 12 Feb 2015
Edited: Stephen23 on 13 Feb 2015
Perhaps I'm using the wrong terminology, but I can't seem to find anything in the help manual or online on how to create nested lists. I'm trying to create a list to track the movements of a series of objects that would look something like this if written in Python:
objectLocations = [[activeA, [[x0a, y0a],[x1a,y1a],...,[xna,yna]]],[activeB, [[x0b, y0b],[x1b,y1b],...,[xnb,ynb]]]]
That list would cover two moving objects, but I want to be able to cover any number of objects at any number of locations and can't seem to find an efficient way to do that. When I try interpreting the list, everything is automatically denested and interpreted as a 1D array.
If anyone could tell me what I'm doing wrong or direct me to the appropriate section of the help manual I'd really appreciate it.
  1 Comment
Stephen23
Stephen23 on 12 Feb 2015
Edited: Stephen23 on 13 Feb 2015
One of the most useful help pages when you are learning MATLAB is the special characters . Actually the square brackets [] are a concatenation operator, rather than a "list" operator. So [1,[2,3]] first concatenates the numeric scalars 2 and 3 to create a numeric vector [2,3], and then concatenates this with the scalar 1 to finally get [1,2,3].
This concatentation operation also works with other classes of data (cell arrays, structures, etc), and does not create a "list" of those objects, but simply concatenates them together giving one larger object of the same class.

Sign in to comment.

Answers (2)

Stephen23
Stephen23 on 12 Feb 2015
Edited: Stephen23 on 12 Feb 2015
MATLAB does not use the term "list" for any data class, although it does refer to comma separated lists (but these are not data storage).
If you want to store nested data, then you need to learn about cell arrays , which are basically like lists in Python. There is one major difference: cell arrays can be matrices or even multi-dimensional.
You might also like to learn about structures , which offer key-value storage.
Other advice for Python users: in MATLAB it much better to use the dimensions of a matrix, rather than nesting data in several layers of "lists". It will make your life much easier too, because MATLAB has many tools for operating on whole matrices at once (this is called vectorization), and doing this is something that makes MATLAB code fast and neat. Vectorization is in many ways one of the main strengths of MATLAB, so it is a shame not to learn how to use it properly.
A few practical tips for planning your data a little bit differently to Python:
  • Use numeric arrays for your data and keep all of your data together as much as possible (no nesting, no key-value, no individual variables).
  • Use the dimensions of an array. Make it with 20 dimensions, MATLAB does not care.
  • Learn about vectorized code. Loops are your second choice, not the first choice.
  • Don't try to keep your meta-data directly with your numeric data. It may be much neater to keep the meta-data in a structure or cell array, and the numeric data in a numeric matrix.
  • Learn about MATLAB's rather powerful indexing ability. There are different ways to index, and they all have different situations where they are useful. Oh, and it has one-based indexing, which is what mathematicians use.
  • The documentation is really good, learn to navigate it using the "Contents" (on the left-hand side on the website): there you can discover many new functions and lots of information.
  • Learn about the different indexing for cell arrays .

Kelly Kearney
Kelly Kearney on 12 Feb 2015
Edited: Kelly Kearney on 12 Feb 2015
I think you want cell arrays:
objectLocations = {{activeA, [x0a, y0a], [x1a, y1a]}, ...
{activeB, [x0b, y0b], [x1b, y1b]}};

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!