2

In many cases I have seen that MATLAB would return empty objects and if you look at their size, they would be something like 1 x 0 or0 x 1. An example is the following piece of code :

img = zeros(256); % Create a square zero image of dimension 256 X 256
regions = detectMSERFeatures(img); 
size(regions)

If you look at the size of regions, it will by 0 X 1. My questions are the following. Some of these questions can be overlapping.

  1. What is the meaning of such dimensions ?
  2. What can be said about the memory layout of such objects ? The reason I am asking about memory layout is because MATLAB allows you to write the following statement: temp = zeros(1,0);
  3. Why can't MATLAB simply return an empty constant like NULL in such cases instead of returning weirdoes of size 1 x 0 ?
Ujjwal Aryan
  • 3,827
  • 3
  • 20
  • 31
  • 2
    Take a look at [this post](http://stackoverflow.com/questions/7746202/difference-between-and-1x0-in-matlab) – Santhan Salai May 07 '15 at 13:38
  • 1
    A 0xN matrix can still be appended, concatenated, used as index to another matrix etc. without throwing an error, as would a NULL object. It's much like having a variable and allowing its value to be 0 - you can still use it in additions etc. – xenoclast May 07 '15 at 13:41
  • 1
    and in addition to the good comments above, these exotic `null` value still retain their type, which can be checked. It is extremely useful in a language which allow _catch all_ function input like `varargin`. Take a look at the code of some of the advanced Matlab functions ... the parsing of the input `varargin` is full of type detection, which has to be effective even if the variable is empty. – Hoki May 07 '15 at 13:47
  • @gnovice is this really a duplicate? OP was asking about memory storage, and the difference between empty arrays and a NULL element, which don't seem covered by the duplicated question. – Sam Roberts May 07 '15 at 15:39
  • @SamRoberts: With regard to memory storage, there's not much else to say than "they're treated like any other array". The implications of this are covered well by the other answers (how it affects matrix multiplication, concatenation, etc.). Also, there's no real NULL in MATLAB unless you count `[]`, and the differences between `[]` and 1-by-0 or 0-by-1 arrays are covered there as well. – gnovice May 07 '15 at 15:52

1 Answers1

1

Arrays in MATLAB can have any of their dimensions of size zero - I guess that may seem odd initially, but they're just arrays like any other.

You can create them directly:

>> a = double.empty(2,0,3,0,2)
a =
   Empty array: 2-by-0-by-3-by-0-by-2

or using other array creation functions such as zeros, ones, rand and so on.

Note that, as is obvious from the above, empty arrays still have a class - you can create them with double.empty, uint8.empty, logical.empty and so on. The same is also true for user-defined classes.

It's very useful to have such arrays, rather than just a NULL element. Without them, you would need to spend a lot of programming effort to check for edge cases where you had a NULL rather than an array, and you wouldn't be able to distinguish between arrays that were NULL because they had no rows, and arrays that were NULL because they had no columns.

In addition, they're useful for initializing arrays. For example, let's say you have an array that needs to start empty but get filled later, and you know that it's always going to have three rows but a variable number of columns. You can then initialize it as double.empty(3,0), and you know that your initial value will always pass any checks on the number of rows your array has. That wouldn't work if you initialized it to [] (which is zero by zero), or to a NULL element.

Finally, you can also multiply them in the same way as non-empty arrays. It may be surprising to you that:

>> a = double.empty(2,0)
a =
   Empty matrix: 2-by-0
>> b = double.empty(0,3)
b =
   Empty matrix: 0-by-3
>> a*b
ans =
     0     0     0
     0     0     0

but if you think it through, it's just a logical and necessary application/extension of the regular rules for matrix multiplication.

As to how they're stored in memory - again, they're stored just like regular MATLAB arrays. I can't recall the exact details (look in the documentation for mxArray), but it's basically a header giving the dimensions (some of which may be zero), followed by a list of the elements in column-major order (which in this case is an empty list).

Sam Roberts
  • 23,951
  • 1
  • 40
  • 64