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.
相关问题
- Extract matrix elements using a vector of column i
- Do the Java Integer and Double objects have unnece
- Reshape matrix by rows
- How to get the maximum of more than 2 numbers in V
- Faster loop: foreach vs some (performance of jsper
相关文章
- 关于C#中 float、double、decimal 的运算不精确的问题。
- Numpy matrix of coordinates
- PHP: Can an array have an array as a key in a key-
- Accessing an array element when returning from a f
- How can I convert a PHP function's parameter l
- How to make a custom list deserializer in Gson?
- How do I append metadata to an image in Matlab?
- Recursively replace keys in an array
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:
So, the class of all these 5 different arrays is
double
, as reported byclass
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 alsomat2cell
andnum2cell
commands available.All cell arrays created above are 1x1 cell arrays.
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:
But this fails:
But let's try a different kind of a cell array consisting of different arrays:
This cell array neither cannot be converted to an array by using
cell2mat
:Hope this helps to get an idea.
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:
which produces the following output:
A
of doubles takes 40 bytes in memory (each double takes 8 bytes).A
with a single cell to produceB
adds extra 112 bytes. That's the overhead of a single cell in MATLAB.C
contains 5 cells and takes (112+8)×5 = 600 bytes.