4

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.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • 2
    It's the same as `cell_array = cell(0, 1)` i.e. making a 0x1 empty cell matrix but I can't say I know any reason for doing this. You could for example make a 0x0 cell array like this `cell_array = {}` if you're just initialising for a loop – Dan Aug 07 '13 at 15:19
  • 1
    @Dan. When you say `0x1`. Does that literally mean `0` rows and `1` column? How can we have `0` rows? Thanks – Simplicity Aug 07 '13 at 15:22
  • 1
    Yes, that's exactly right. I don't know why you would need this. 0x0, i.e. empty is useful. 0x1 is pretty much just as empty to me though :/ – Dan Aug 07 '13 at 15:23
  • An empty cell array lets you have subsequent codes to concatenate to it. Otherwise, the variable does not exist, which produces an error. – radarhead Aug 07 '13 at 15:30
  • @radarhead so why not just `{}` then or `cell([],[])` or `cell(0,0)`. Why 0x1 instead of 0x0? – Dan Aug 07 '13 at 15:33
  • 1
    I can think of one. Say the subsequent concatenation wants to check the number of columns to make sure they match before merging them. It's a way to preset the number of columns if you will. – radarhead Aug 07 '13 at 15:40
  • @radarhead That's a good observation! – Dan Aug 08 '13 at 11:45

2 Answers2

2

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.

Community
  • 1
  • 1
Dan
  • 45,079
  • 17
  • 88
  • 157
0

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);
Nick
  • 3,143
  • 20
  • 34