Accessing data in cell arrays
Show older comments
Hi,
I have a basic question to which I can't find an answer.
Suppose we have a cell matrix:
C = cell(2,2);
What is the difference between
C{1,2} = 2;
and
C{1}{2} = 2;
?
Thanks in advance,
KK
Accepted Answer
More Answers (1)
Simplest way to learn these things is to try it on the command line and see what the result is.
C = cell(2,2)
C =
[] []
[] []
>> C{1,2} = 2
C =
[] [2]
[] []
C2 = cell(2,2)
C2 =
[] []
[] []
>> C2{1}{2} = 2
C2 =
{1x2 cell} []
[] []
The first syntax is the standard one which does exactly what you expect, i.e. it puts the value 2 into the cell indexed at row 1, column 2. Thus:
C{1,2}
will return the value 2 as a double and
C(1,2)
would return a 1*1 cell array containing the single value 2.
The second syntax is one I never use as it is rather less intuitive and not useful for me. It will index into the first cell of the cell array using 1d indexing and then further use 1d indexing to create a cell array of 1x2, the second element of which is the 2 that you specify.
8 Comments
DrKarimKecir
on 16 Feb 2015
DrKarimKecir
on 16 Feb 2015
Yes, the
x{5} = [];
or
x{5} = 2;
is a standard and quite useful way to create an array of a certain size, automatically initialising all intermediate elements to default values.
I use that a lot in object-oriented code for pre-allocating object arrays.
Each of these indexing methods is useful in different situations, so if you dismiss them as being "not important" then you cut yourself off from some very handy indexing tools. Particularly for indexing cell arrays , where sometimes you need to refer to the contents of the cells, and sometimes to the cells themselves. Both of these are useful in different situations.
As a simple example, if you have a cell array of strings, and you want to concatenate them together with commas and spaces:
>> C = {'anna','bob','cathy','doug'};
>> C(2,:) = {', '}; % note the parentheses!
>> [C{1:end-1}]
ans = 'anna, bob, cathy, doug'
This allocates a scalar cell array to multiple cells of another cell array, exactly like you can do with numeric scalars and arrays:
A = 1:6;
A(2,:) = 9;
Using x(n) = ... is one of the fastest ways to initialize and preallocate an array in MATLAB. In fact the documentation explicitly states these two methods of initializing a cell array:
C = cell(25,50);
C{25,50} = [];
Other code that utilizes this trick is to allocate an array in a loop, looping over the indices in reverse:
for k = N:-1:1
myArray(k) = scalar_value;
end
DrKarimKecir
on 17 Feb 2015
Edited: DrKarimKecir
on 17 Feb 2015
To get the best performance out of MATLAB you want to not keep resizing your variables. For example this code enlarges the vector on every iteration:
A = [];
for k = 1:1e4;
A(k) = sin(k);
end
Whereas iterating in reverse will create the whole vector on the first iteration, after which it does not change size again:
A = [];
for k = 1e4:-1:1
A(k) = sin(k);
end
Alternatively you can preallocate the vector and iterate in the usual direction:
A = nan(1,1e4);
for k = 1:1e4
A(k) = sin(k);
end
Have a play with tic and toc, you might be surprised how much faster well written code can be in MATLAB.
DrKarimKecir
on 17 Feb 2015
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!