-2
  • Background of the Issue

I ask a question What situations do we need to use the empty method? What benefits does the empty method provide?

I got an answer.But more answer more question.

This answer provides two reference questions :Difference between [] and [1x0] in MATLABWhy can an empty array have a non-null dimension?(to avoid excessive clutter, only links are provided here).

These two reference questions and their answers have led to more contemplation and confusion for me. So, I posed this question in the hope that computer professionals could provide a more fundamental answer from the computer's lower levels (such as the operating system and memory.), allowing amateurs like me to understand what exactly happens in computer memory.


  • Question

See this: What situations do we need to use the empty method? What benefits does the empty method provide?

When executing A = ColorInRGB.empty(5,0); Did I get five empty rooms?

When I execute A = ColorInRGB.empty(5, 0), it shows that A's memory usage is 0. However, when I execute A = ColorInRGB.empty(1000000000000000000000000000, 0), it prompts an error: Error using ColorInRGB.empty. Requested array exceeds maximum variable size.

Since the created A has a memory usage of 0, why does it give an out-of-memory error when increasing the number of rows? why the computer provides an n×0 empty. what situations should I need to use an n×0 empty?


  • Another Related Question

Especially this answer, why does b*a result in []? why does a*b result in 0?


I'm not sure if the tags for this question are categorized correctly. If you find that the tags for this question are incorrect, please assign the correct tags to it(Except for the 'Matlab' Tag). Thank you.


2023/08/29

Thanks Martin Rosenau.This is a follow-up question regarding Martin Rosenau's response.

1 I think Matlab will internally store something like this -----I find this perspective very reasonable. I speculate whether MATLAB's built-in classes might have some built-in hidden properties that the empty method cannot modify? Here are the reasons I've listed:

A=double.empty(5,0);
B=double.empty(0,5);
C=A*B
C =

 0     0     0     0     0
 0     0     0     0     0
 0     0     0     0     0
 0     0     0     0     0
 0     0     0     0     0

However, when I create a class and use it to build an empty array, then multiply them...

classdef ColorInRGB<handle
    properties
        Color; 
    end

end

 A=ColorInRGB.empty(5,0);
 B=ColorInRGB.empty(0,5);
 C=A*B
'ColorInRGB' 类型的操作数不支持运算符 '*'。
                    

I speculate that the empty method might be altering some properties of the object, causing the matrix multiplication to fail. However, I lack evidence. I wonder if there's a way to make the ColorInRGB object capable of performing empty array multiplication, similar to how double objects can.

2、

Matlab will internally store something like this in this case
myVariable.dataType = 'double'
myVariable.numberOfDimensions = 2
myVariable.dimensions = [0 5]
myVariable.values = [ ]

---I think your speculation makes a lot of sense. Otherwise, it's hard to explain why the workspace shows the dimension of an empty array as 5*0. However, I've tried various methods and can't seem to find the dimension property for double objects. I'm not sure if there's another way.Here are the steps I've attempted:

A=double.empty(5,0);
  properties(A)
  mc=?A;
  mc

2 Answers2

4

please assign the correct tags to it(Except for the Tag).

Your problem is related only to Matlab:

C# and Java are the only languages I know that allow something similar to a [Nx0] array but nothing similar to a [0xN] array.

Other programming languages I know are not able to do something similar.

So, I posed this question in the hope that computer professionals could provide a more fundamental answer from the computer's lower levels ...

The hardware of the computer does not know about data types and array sizes. For this reason, the programming language (in the case of an interpreter) or the compiled program must "know" what data type and array size some variable has.

When you type the following line in Matlab (a 2x3 matrix):

myVariable = [ 1 2 3 ; 4 5 6]

... I think Matlab will internally store something like this:

myVariable.dataType = 'double'
myVariable.numberOfDimensions = 2
myVariable.dimensions = [2 3]
myVariable.values = [ 1 2 3 4 5 6 ]

... because Matlab needs the information about the data type, the array size etc...

Why can an empty array have a non-null dimension?

Matlab will internally store something like this in this case:

myVariable.dataType = 'double'
myVariable.numberOfDimensions = 2
myVariable.dimensions = [0 5]
myVariable.values = [ ]

As you can see, dataType, numberOfDimensions and dimensions do contain some information in this case. For this reason, the "empty" array (myVariable) requires memory in the computer (even in the case of a 0x0 array).

What situations do we need to use the empty method? What benefits does the empty method provide?

You can easily create an array of a certain size of a certain data type:

If you just write [], you get a 0x0 array of the data type double. If you require some 0x0 (or Nx0 or 0xN) array of the data type ColorInRGB, you use the empty method.

When I execute A = ColorInRGB.empty(5, 0), it shows that A's memory usage is 0.

As I have mentioned above, A also requires some memory in this case for storing the data type and the array size.

Requested array exceeds maximum variable size.

Interpreting "ctrl-alt-delor"'s answer, I think that the dimension size (myVariable.dimensions in the example above) is stored as array of 48 bit integers.

This has the consequence that Matlab can only store numbers in the range from 0 to 2^48-1 in myVariable.dimensions. And for this reason, larger array dimensions are not possible.

However, this has nothing to do with "out-of-memory" but only with the way Matlab stores the array sizes.

What situations should I need to use an N×0 empty?

In a situation where you require a certain number of rows.

Example: You want to multiply a MxN matrix and an empty matrx. MxN matrices can only be multiplied with NxK matrices, so you cannot multiply a MxN matrix with a 0x0 matrix but only with an Nx0 matrix.

Especially this answer, why does b*a result in []? why does a*b result in 0?

Because when multiplying a NxM matrix with an MxN matrix, the result is an NxN matrix.

For this reason, multiplying a 0xM matrix with an Mx0 matrix results in a 0x0 matrix.

... and multiplying a Mx0 matrix with a 0xM matrix results in a MxM matrix.

Now in Matlab, a number is nothing but a 1x1 matrix, so 0 is an 1x1 matrix filled with zeros.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38
  • Thank you very much for your detailed answer. It has benefited me a lot. May I ask whether your above knowledge comes from matlab documents or matlab books? Do you have any book or document recommendations? I also want to learn some related knowledge. Also, in response to your answer, I added two questions to the post (because code formatting is not supported in comments). These two questions would also like to hear your opinion. – bokabokaboka Aug 29 '23 at 04:12
  • Array sizes are stored as 64-bit integers, 48-bit integers don’t exist, at least not in a x86_64 architecture. But MATLAB uses doubles for everything, it’s also the return type of the `size` function. So sizes can’t be larger than 2^53. I guess 2^48-1 is a nice “round” limit? Maybe the upper bits are used for something else? And MATLAB is column-major. Other than these details, this is a good answer. – Cris Luengo Aug 29 '23 at 04:43
  • @CrisLuengo Not knowing the internals of Matlab, I only guessed that 48-bit integers are used based on the answer already given. And there are known examples of arrays of integers with "weird" lengths: The FAT12 file system stores information in an array of 12-bit integers although it was developed for the i8080 and later used on x86 and ARM and neither of these CPUs have hardware support for 12-bit integers. – Martin Rosenau Aug 29 '23 at 05:13
  • 1
    @bokabokaboka About 15 years ago, I was working with Matlab professionally. This is where my information about the "non-internals" of Matlab ("why do you need a Nx0 matrix ...") comes from. I have worked with a lot of programming languages where variables don't have fixed data types and fixed array sizes. And I was even developing compilers myself. All these programming languages have the same problem: The CPU does not know the data type nor the array size. And all languages use nearly the same solution for this problem. So I **guessed** that Matlab also does it this way. – Martin Rosenau Aug 29 '23 at 05:35
  • @ChrisLuengo MATLAB stores dimensions internally in the mxArray header as integers, not doubles. E.g., size_t. This despite the fact that the size( ) function returns these values as doubles. – James Tursa Aug 31 '23 at 00:34
  • @MartinRosenau Fortran also allows 0-sized arrays. – James Tursa Aug 31 '23 at 00:39
1

Yes, a rectangle that measures n × 0 has an area of 0.

However, matlab is storing the size of this rectangle (array). Therefore n must be less than or equal to the maximum array size.

Quoted from mathworks — “The maximum number of rows or columns in MATLAB is 281474976710655 which is 2^48-1 . However, the array must fit into memory.”

Your size of 1000000000000000000000000000 is too big. It is OK (as you point out) on the 2nd clause: must fit into memory. But not on the 1st clause: The maximum number of rows or columns.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52