2

The method empty creates an empty array. What situations do we need to use the empty method? What benefits does the empty method provide? Will it lead to efficiency improvements through preallocation? Do they have different implications for the user?

Input:

A = ColorInRGB.empty(1,0);
B = ColorInRGB1();

A is 1x0 ColorInRGB, memorysize=0.
B is 1x1 ColorInRGB, memorysize=0. workspace

What's the difference between A and B? What's the distinction between an empty 1x0 and an empty 1x1? Or is there any distinction between a 5x0 and a 1x1 empty? If there's no difference, why do we use the empty method?

ColorInRGB.m

classdef ColorInRGB
   properties
      Color (1,3) = [1,0,0];
   end
   methods
      function obj = ColorInRGB(c)
         if nargin > 0
            obj.Color = c;
         end
      end
   end
end

ColorInRGB1.m

classdef ColorInRGB1
    properties
        Color; % (1,3) = [1,0,0];
    end
    methods
%         function obj = ColorInRGB(c)
%             if nargin > 0
%                 obj.Color = c;
%             end
%         end
    end
end

2023/08/27 12:59 These two classes are different. My question is, ColorInRGB1() can help me obtain an empty array, so why do we still need an 'empty method'? Once I know the differences between them, I can understand when to use ColorInRGB1() in what scenarios and when to use ColorInRGB.empty(1,0); in others.

  • Your stated outputs are wrong, which makes this look more confusing than it is. `ColorInRGB.empty(0,5)` returns a `0x5 ColorInRGB array`, i.e. it has the correct size not `1x0` as you've stated. This is useful when you have multiple variables which should have some consistency in size so you can operate between them or have more validation / more robust expected behaviour for example looping over the non-zero dimension. Also from the docs _This method is useful for creating empty arrays of data types that do not have a special syntax for creating empty arrays, such as [] for double arrays._ – Wolfie Aug 25 '23 at 07:16
  • @Wolfie I'm sorry, that was my mistake. The code should be A = ColorInRGB.empty(1,0) instead of A = ColorInRGB.empty(0,5). Thank you for pointing out the error, I have updated the question. – bokabokaboka Aug 25 '23 at 11:42
  • The biggest difference between `A` and `B` is that they are of a different class. This muddles the question a bit. Other than that, `isempty(A)` is true whereas `isempty(B)` is false. – Cris Luengo Aug 25 '23 at 13:46
  • @CrisLuengo These two classes are indeed different. My question is, `ColorInRGB1()` can help me obtain an empty array, so why do we still need an `empty` method? Of course, you mentioned that "isempty(A) is true" is a significant difference. But I also want to know if there are any other differences. Once I know the differences between them, I can understand when to use `ColorInRGB1()` in what scenarios and when to use `ColorInRGB.empty(1,0);` in others. – bokabokaboka Aug 27 '23 at 04:57
  • @CrisLuengo Thank you for your help.memorysize is a column of workspace, showing the memory it occupies. For example, the space occupied by x=['abad''1235''asdf'] is 1x14. I use the Chinese version of matlab, I don't know what is the name of this column in the English version? – bokabokaboka Aug 27 '23 at 13:00

1 Answers1

0

B is a 1x1 object array whose only field is an empty array. A is a 1x0 object, it has no elements.

B is not an empty array, but it contains an empty array. A is an empty array.

I understand why the distinction is not directly obvious. Both contain no data (this is the “memory size” reported by the workspace explorer and the whos command, which doesn’t include the overhead of the object itself). But there are clear differences:

  • The isempty test is true for A but not for B.
  • We can index into B: B(1) is valid, A(1) is not. B.Color is valid, as is the assignment B.Color = 'foo'; you can do neither with A.

This is exactly the same as the difference between the empty cell array {} and the 1x1 cell array {[]}, which contains an empty array.


If you are curious about why empty can create a 1x0 array, as opposed to only a 0x0 array, and why we want empty arrays at all, see this question and this other one.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120