Cell in cell in cell...... versus multiDimensional cell

Hi all,
Imagine now I have some data to be stored, say there are 24 matrices I need to store. I have 2 choices:
1. use cell in cell, e.g.
>> a
a =
{3x1 cell}
{3x1 cell}
>> a{1}
ans =
{4x1 cell}
{4x1 cell}
{4x1 cell}
>> a{1}{1}
ans =
[]
[]
[]
[]
2. use a multiDimensional cell, e.g.
>> a = cell(2, 3, 4)
a(:,:,1) =
[] [] []
[] [] []
a(:,:,2) =
[] [] []
[] [] []
a(:,:,3) =
[] [] []
[] [] []
a(:,:,4) =
[] [] []
[] [] []
and store each matrix in each cell element.
Which one is better and why? Personally I like the second way because it's clearer. The first way always confuses me.

1 comentario

KSSV
KSSV el 27 de Mzo. de 2017
How you store depends on how you are access at the time of calculation. If you want to use two loops/ two dimensional then matrix wise is better. If only one loop then column or row wise cell is better.

Iniciar sesión para comentar.

 Respuesta aceptada

Stephen23
Stephen23 el 27 de Mzo. de 2017
Editada: Stephen23 el 27 de Mzo. de 2017

2 votos

Assuming that the subarrays can be joined together in one multi-dimensional array, then the multi-dimensional array is better. Some reasons are:
  • you can use indexing to access elements -> faster and neater code.
  • you can use vectorized code -> faster and neater code.
  • simpler data arrangement -> simpler code -> fewer bugs.
  • more generalized code -> more adaptable code -> more useful code.
  • lower memory requirements.
  • fewer brackets -> easier to read -> fewer bugs.
  • does not require loops.
However if the data arrays have quite different sizes or types then nested arrays may be appropriate. Planning how to arrange data is just like planning how to write code: simpler code is better than more complicated code that performs the same function, and likewise for your data: Keep It Simple Stupid.
Summary: in general the simpler the data storage the better the code will be.

5 comentarios

Xh Du
Xh Du el 27 de Mzo. de 2017
Just to confirm multi-dimensional array is the same as multi-dimensional cell in this case, is this correct? I'm asking because I think in MATLAB array and cell are usually different.
Guillaume
Guillaume el 27 de Mzo. de 2017
Editada: Guillaume el 27 de Mzo. de 2017
Stephen is using the term array to mean both cell arrays and standard numerical arrays. Note that the name is cell array, not just cell.
There is actually not that much difference between a cell array and plain numerical array (matrix). Both are containers, the cell array contains pointer, the standard array contains numbers. Lots of functions operate the same on both (e.g. size, subsref, etc.)
Regarding your question, I agree with Stephen and Jan. Use whichever is simpler. You are right to be asking this sort of questions. Good design of your data structure will lead to good code design.
One important thing that I don't think has been mentioned. Whichever design you choose, make sure that it is documented extensively.
Xh Du
Xh Du el 27 de Mzo. de 2017
Hi Guillaume,
Thanks, can you be more specific about 'documented extensively'?
Stephen23
Stephen23 el 27 de Mzo. de 2017
"documented extensively" means that you need to explain what your code is doing: code comments are part of this, as are clearly describing input and output arguments of functions. For an N-dimensional array "documented extensively" would probably include explaining its type, what each of its dimensions represent, and anything relevant about the data.
Xh Du
Xh Du el 27 de Mzo. de 2017
Thanks I got it.

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 27 de Mzo. de 2017
Editada: Jan el 27 de Mzo. de 2017
If the first way confuses you, the question is answered already: Stay away from confusing solutions.
Efficiency is important for programming. You can define it as the time required to solve a problem:
Total time = design time + programming time + debug time + run time
If the run time is optimized on the costs of the programming and debug time, the total time might grow. So even if the first method would be faster, the reduced clarity is a knock out criterion.
The decision for a clear and efficient data representation is art. Experiences are required to decide this. Frequently, the demands for a data structure changes during larger projects. Therefore it is a good programming practice to keep the data access separated ("encapsulated"), such that it can be exchanged easily.

1 comentario

Xh Du
Xh Du el 27 de Mzo. de 2017
I would like to accept your answer too but since I cannot do it I'll vote up for yours.

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 27 de Mzo. de 2017

Comentada:

el 27 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by