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

眉间皱痕 提交于 2019-12-24 17:04:46

问题


I have two variables who look exactly the same to me, but one is <double> and the other is <cell>. In the code it seems that they are converted by cell2mat. I understand it is a question of data storage but I just don't see the difference and the definition of cell and double for this.


回答1:


Adding to nrz's answer, it is noteworthy that there is an additional memory overhead when storing cell arrays. For instance, 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 of doubles takes 40 bytes in memory (each double takes 8 bytes).
  • The second line shows that just wrapping A with a single cell to produce B adds extra 112 bytes. That's the overhead of a single cell in MATLAB.
  • The third line confirms that, because C contains 5 cells and takes (112+8)×5 = 600 bytes.



回答2:


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

1D and 2D arrays are matrices just like in mathematics, in linear algebra. But arrays can also be multidimensional (n-dimensional) arrays, also called tensors, MATLAB calls them multidimensional arrays. Further, MATLAB does not make any distinction between scalars and arrays, nor between vectors and other matrices. A scalar is just 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 class command. double means the numeric precision used (double-precision).

The cell array is a more abstract concept. A cell array can hold one or more arrays, it can also hold other types of variables that are not arrays. A cell array can also hold other cell arrays which can again hold whatever a cell array can hold. So, cell arrays can also be stored recursively inside one another.

Cell arrays are useful for combining different objects into a single variable that can eg. be passed to a function or handled with cellfun. Each cell array consists of 1 or more cells. Any array can be converted to cell array using { } operators, the result is a 1x1 cell array. There are also mat2cell and num2cell commands 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 converted into matrices using cell2mat, because a single cell array can hold several different data types that cannot exist in the same array.

These do 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 let's try a different kind of a 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 cell array neither cannot be converted to an array by 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 get an idea.



来源:https://stackoverflow.com/questions/14081418/what-is-the-difference-between-the-data-stored-in-a-cell-and-the-data-stored-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!