Simplicity
Simplicity

Reputation: 48916

Cell arrays in MATLAB

I know what a cell array is. I just came to the following line:

cell_array = cell([], 1);

What does that mean? How can we read the above line?

Thanks.

Upvotes: 4

Views: 770

Answers (2)

Dan
Dan

Reputation: 45741

So this makes a 0x1 empty cell array. As in literally 0 rows and 1 column. You could make a 0x0 cell array like this:

cell_array = {}

which makes sense to me. I do that sometimes like if you can't preallocate before a loop, sometimes it's useful to concatenate onto or to go cell_array(end) = ... in the loop.

I really don't know why you'd prefer a 0x1 but this question shows how it differs from a 0x0. So I mean, if for some weird reason you are running a for loop on this empty array you know it will run at least once :/ But that's really scraping the barrel for a reason. I think just stick with = {}.

EDIT:

As @radarhead points out, this is one way to preset the number of columns if you plan on concatenating new rows in a loop.

Upvotes: 2

Nick
Nick

Reputation: 3193

cell is used to allocate a cell array, in this case: cell_array.

So an empty cell of size 0x1 is allocated. However, this is no usefull syntax for cells. For other (custom) objects it can be useful to generate such empty arrays

myArray = myObject.empty(0, 1);

Upvotes: 0

Related Questions