cell array or multidimensional array?

15 ビュー (過去 30 日間)
AP
AP 2011 年 5 月 19 日
Which of the two is more MATLAB-friendly?
  1. Cell Array, or
  2. Multidimensional Array
If cell arrays are better than multidimensional arrays, how should I define the following array in cell array format:
A(128, 128, 17, 4, 25)
Suppose A is:
A(X, Y, Z, VARS, TIMES)
which means A contains the value of some variables VAR1, VAR2, ..., VARN in a 3D space in different times.

採用された回答

James Tursa
James Tursa 2011 年 5 月 19 日
Depends on what you are doing with the data downstream. If you are accessing array slices of A a lot, then the cell array approach might be more efficient because it would not involve data copying to access the slices (whereas accessing a slice of an nD array does involve a data copy each time you do it). But if you need to work with the data contiguously downstream then the nD array approach might be more efficient (rather than cat-ing the cell arrays together which would involve a data copy).
  2 件のコメント
Andy
Andy 2011 年 5 月 19 日
Perhaps I'm misunderstanding you, but this does not appear to be the case on my machine:
x = rand(100,100);
numruns = 1e5;
rows = 50:60;
cols = 50:60;
tic
for ix=1:numruns
x(rows,cols);
end
toc
x = num2cell(x);
tic
for ix=1:numruns
x(rows,cols); % <- parens
end
toc
tic
for ix=1:numruns
x{rows,cols}; % <- braces
end
toc
Elapsed time is 0.129221 seconds.
Elapsed time is 0.211889 seconds.
Elapsed time is 2.395911 seconds.
Sean de Wolski
Sean de Wolski 2011 年 5 月 19 日
Andy: The data copy is only pulling 121 elements or less than a kilobyte.
The memory required for double matrix of the OP's size:
>> prod([128, 128, 17, 4, 25])*8
ans = 222822400
If copy's of this are necessary then memory copy could become a time sink, especially if the RAM limits of the computer are approached.

サインインしてコメントする。

その他の回答 (2 件)

Andy
Andy 2011 年 5 月 19 日
Obviously it depends: is all of your data numeric, or are the data types mixed? If the data is all numeric, then I'd say stick with a numeric array. If it's mixed, use a cell array.
EDIT (more explanation): There are lots of functions designed to manipulate numeric data stored in an array (all of the operators, mathematical functions, etc.). If you store your data in a cell array, you need to convert back to a numeric array every time you need to calculate, say, the mean of your data. I cannot think of any functions that operate on cell arrays of numeric data that don't also operate on numeric arrays of numeric data. Also, cell arrays introduce lots of opportunity for hard to find typos (like typing myCell(1) instead of myCell{1}).
You should use cell arrays only when you really have to. That is when you have data of mixed type or arrays of different sizes. Otherwise, stick with numeric arrays.
  1 件のコメント
Sean de Wolski
Sean de Wolski 2011 年 5 月 19 日
+1

サインインしてコメントする。


Oleg Komarov
Oleg Komarov 2011 年 5 月 19 日
Cell arrays have significant overhead and they are slower wrt do a double nD array but they can mix datatypes.

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by