What is the difference between data stored in a cell and data stored as double in MATLAB?

I have two variables that look the same for me, but one is <double> and the other is <cell> . In the code, it seems like they are being converted by cell2mat . I understand that this is a matter of data storage, but I just do not see the difference and the definition of cell and double for this.

+1
source share
2 answers

Adding to the nrz answer, it should be noted that when storing arrays of cells there is additional memory overhead. For example, consider the following code:

 A = 1:5 B = {A} C = num2cell(A) whos 

which produces the following output:

 A = 1 2 3 4 5 B = [1x5 double] C = [1] [2] [3] [4] [5] Name Size Bytes Class Attributes A 1x5 40 double B 1x1 152 cell C 1x5 600 cell 
  • As you can see from the first line, the basic 1-by-5 ​​vector A double takes 40 bytes in memory (each double takes 8 bytes).
  • The second line shows that just wrapping A with one cell to create B adds an extra 112 bytes. This is the overhead of a single cell in MATLAB.
  • The third line confirms that since C contains 5 cells and occupies (112 + 8) Γ— 5 = 600 bytes.
+5
source

Arrays and cell arrays are probably the two most commonly used data types in MATLAB.

1D and 2D arrays are matrices, as in mathematics, in linear algebra. But arrays can also be multidimensional (n-dimensional) arrays, also called tensors, MATLAB calls them multidimensional arrays. In addition, MATLAB makes no distinction between scalars and arrays, as well as between vectors and other matrices. A scalar is only a 1x1 array in MATLAB, and vectors are Nx1 and 1xN arrays in MATLAB.

Some examples:

 MyScalar = 1; MyHorizVector = [ 1 2 3 ]; MyVertVector = [ 1 2 3 ]'; MyMatrix = [ 1, 2; 3, 4 ]; My4Darray = cat(4, [ 1 2; 3 4], [ 5 6; 7 8 ], [ 9 10; 11 12 ], [ 13 14; 15 16 ]); class(MyScalar) ans = double class(MyHorizVector) ans = double class(MyVertVector) ans = double class(MyMatrix) ans = double class(My4Darray) ans = double 

So, the class of all these 5 different arrays is double , as reported by the class command. double means the numerical precision used (double precision).

A cell array is a more abstract concept. A cell array can contain one or more arrays, it can also contain other types of variables that are not arrays. An array of cells can also contain other arrays of cells, which can again hold everything that an array of cells can contain. In this way, cell arrays can also be stored recursively inside each other.

Array cells are useful for combining different objects into one variable, which can, for example. passed functions or processed using cellfun . Each cell array consists of 1 or more cells. Any array can be converted into an array of cells using the { } operators, the result will be an array of 1x1 cells. The mat2cell and num2cell commands are also available.

 MyCellArrayContainingMyScalar = { MyScalar }; MyCellArrayContainingMyHorizVector = { MyHorizVector }; MyCellArrayContainingMyCellArrayContainingMyScalar = { MyCellArrayContainingMyScalar }; 

All cell arrays created above are 1x1 cell arrays.

 class(MyCellArrayContainingMyScalar) ans = cell class(MyCellArrayContainingMyHorizVector) ans = cell class(MyCellArrayContainingMyCellArrayContainingMyScalar) ans = cell 

But not all cell arrays can be transformed into matrices using cell2mat , because a single cell array can contain several different data types that cannot exist in a single array.

They work:

 cell2mat(MyCellArrayContainingMyScalar) ans = 1 cell2mat(MyCellArrayContainingMyHorizVector) ans = 1 2 3 

But this fails:

 cell2mat(MyCellArrayContainingMyCellArrayContainingMyScalar); Error using cell2mat (line 53) Cannot support cell arrays containing cell arrays or objects. 

But try a different type of cell array, consisting of different arrays:

  MyCellArray{1} = [ 1 2 3 ]; MyCellArray{2} = 'This is the 2nd cell of MyCellArray!'; class(MyCellArray) ans = cell 

This array of cells cannot be converted to an array using cell2mat :

 cell2mat(MyCellArray) Error using cell2mat (line 46) All contents of the input cell array must be of the same data type. 

Hope this helps to understand the idea.

+4
source

Source: https://habr.com/ru/post/950255/


All Articles