is there a way to give suggestions to Mathworks

Hi,
I stumbled upon a warning mentioning that in future versions of matlab, cell arrays and list (not sure about lists) will be required to have the correct size to be concatenated. We will not be able to concatenate an empty (0x0) cell array with a 1x3 cell array for example.
This warning tells us that we need to create it with the correct number of columns.
Currently there is no (clean) way to create empty arrays and lists. The only way i found was to create it with functions such as zeros(0,n) and ones(0,n).
But these functions are not intended for this use case.
I tried to search a place where i could suggest Matlab to implement a way to initialize empty lists, cell arrays, structures and so on, but I couldn't find this place.
Thus I'm asking the question: Is there a place where we can suggest features to implement in future versions of Matlab?
Thank you!

12 Comments

"Currently there is no (clean) way to create empty arrays and lists."
What about the documentation here, which I found using an internet search taking ca. 0.1 seconds:
which lists several ways of creating empty arrays, stating "You can create empty matrices (and arrays) of other sizes using the zeros, ones, rand, or eye functions." When I looked just now at the documentation for zeros and ones they both state "If the size of any dimension is 0, then X is an empty array", completely without any warning that they are "...not intended for this use case" as you state.
"But these functions are not intended for this use case."
Why not? I can't see this restriction mentioned in the ones or zeros documentation, and so far the only documentation I can find explicitly lists exactly those functions as ways to create empty arrays. Can you please provide a reference for your statement.
Using zeros or ones to create empty arrays with specific dimensions seems reasonable to me: what do you see as the problem with using them?
"I tried to search a place where i could suggest Matlab to implement a way to initialize empty lists, cell arrays, structures and so on, but I couldn't find this place."
Right at the top of this page you will find a link "Contact Us": have you tried that?
Hi, thank you for taking the time to answer. Though I don't get why you seem so aggressive toward my question.
I went in the "contact us" link before asking this question and since I don't want to buy something or need support, the links there were not useful.
I understand that it took you 0.1 seconds to search the web, me as well.
My point for the context of the question was more or so this:
  • There is no dedicated function to build empty objects in Matlab.
  • Usage of existing functions to do so works, but is not well known to a good portion of users. (questions concerning creation of empty lists, arrays, matrix, ect. is not rare on this forum as well as others.)
  • functions such as zeros(), ones(), rand() can only allow creation of column or row empty objects. Maybe there would be some cases where a defined (mxn) empty object would be useful. (I don't have one in mind, it's an hypothesis).
Anyway, again, thank you for your attempt to answer the question!
"Functions such as zeros(), ones(), rand() can only allow creation of column or row empty objects."
Huh? By default the single argument form creates a square array; I will allow as how in some 30 years since first began using Matlab I don't believe I had until just now ever thought of using something like
Z=zeros(0,0,0);
to create an empty array of N dimensions, however; never crossed my mind to do so although I almost never write code that does dynamic concatenation excepting for very simple "throw-away" type applications such as reading a file line-by-line which is known to be sufficiently small as to not make the reallocation overhead excessive.
However, if there is such a need, certainly the functions are there to do so it seems (altho I don't know what you mean by 'list'; there's not a generic list data type in Matlab that I'm aware of; there is the containers.Map class altho I don't know and doc doesn't indicated when it was introduced, I've yet to try to make any use of it).
I had not seen nor run into the above warning; it is, I suppose the ultimate result of trying to avoid dynamic reallocation to make JIT optimizations better plus, perhaps, otherwise improve parsing by not dealing with mismatches other than to just barf.
Can you give specific code construct you have used where you would run into the problem; perhaps that would lead to alternative coding styles/implementation details to avoid the issue.
As for submitting suggestions, use Technical Support; the New Service Request form has option for it at MathworksServiceRequest
Hi! While I can't remember my exact case, I found one that prompted the same warning.
function out = wiferight(in)
a=ones(5,10);
b=ones(5,0);
x=[a;b]
out='yes';
end
x = 'But I''m actually right this time';
y_correct = 'yes';
assert(isequal(wiferight(x),y_correct))
Warning: This concatenation operation includes an empty array with an incorrect number of columns.
Concatenation including empty arrays will require all arrays to have the same number of columns in a
future release.
> In wiferight (line 6)
Well, I can imagine that eventually being disallowed I wonder what's the point of writing such in the first place (unless is totally artificial just to cause the error)? But, I'm having trouble imagining a real-world case.
NB: I notice the link to the SRQ page doesn't take you there directly; I navigated to that point from the Support link trying to shortcut a step or two. You can also just email <mailto:support@mathworks.com support@mathworks.com>; put "Suggestion" or somesuch in subject.
"There is no dedicated function to build empty objects in Matlab."
There would need to be variations for cell arrays, numeric arrays (and each different numeric data type), symbolic arrays, symbolic functions, structs, characters, logical, struct, transfer functions, containers.Map, graph objects, primitive line objects, surf objects, contour objects, uipanel, uicontrol, patch objects, hggroup, hgtransform... and many others.
"b=ones(5,0);"
If you had used b = []; then there would not have been any warning message. You seem to have known enough about how to use ones to know yourself how to create an empty object.
"There is no dedicated function to build empty objects in Matlab."
"functions such as zeros(), ones(), rand() can only allow creation of column or row empty objects."
Not true: ones(4,2,3,0,9) is not a row or column vector, it is a 5D empty array. I can't find any restriction written anywhere that zeros and ones can only create rows and columns.
"Maybe there would be some cases where a defined (mxn) empty object would be useful"
Both the page I linked to in my first comment and also the isempty documentation state that "An empty matrix has one or more dimensions that are equal to zero", which clearly MxN is not (as all trailing dimensions are implicitly one). MATLAB uses the term empty to only refer to the size of an array, not to its contents. One of the dimensions must be zero.
It seems that you perhaps want to preallocate a matrix of a fixed size but without initializing its values: in some languages it is possible to allocate memory of a specific size and class, but without setting its values (unlike ones and zeros), where it might have some small speed advantage. Is this what you are trying to achieve? If so, why do you want to avoid using ones / zeros ? (I can think of one issue with using them in this way, but in order to help you am interested to know what your reason is to avoid using them, so we can help to find you the solution that you are looking for).
For your example you just need to define the two matrices to have the same number of columns:
a = ones(5,10);
b = ones(0,10);
x = [a;b]
and there will be no warning.
@Walter Roberson: according to the warning message, b = [] won't work in future because the number of columns differs. Does this also apply to 0x0 matrices?
No warning for the 0 x 0 case:
>> [ones(5,10); ones(5,0)]
Warning: This concatenation operation includes an empty array with an incorrect number of columns.
Concatenation including empty arrays will require all arrays to have the same number of columns in a future release.
ans =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
>> [ones(5,10); ones(0,0)]
ans =
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1
@Stephen Cobeldick
Indeed, my interest was for speed improvement.
" ones(4,2,3,0,9) is not a row or column vector, it is a 5D empty array. I can't find any restriction written anywhere that zeros and ones can only create rows and columns."
I learned something thank you for sharing.
" Is this what you are trying to achieve? If so, why do you want to avoid using ones / zeros ?"
Yes, and an other reason is that the name of these functions implies that you want to create a matrix filled with zeroes or ones, not an empty matrix. If the name of functions was not important and only the result was important, the reason for the creation of the function ones() could have been discarded because zeroes() existed and after the creation of the array, you could have just added "1" the the matrix, filling it with ones.
"Indeed, my interest was for speed improvement."
You might be interested in downloading this:
I have no idea if that is faster for empty arrays, but it could be worth a try.
"...the name of these functions implies that you want to create a matrix filled with zeroes or ones..."
Fair enough. Perhaps a slight improvement would be to use nan, which at least makes it clear that you do not want a particular numeric value.
A final piece of fun: if you want to blow your mind with crazy things that empty arrays can do, then consider this simple matrix multiplication:
>> nan(2,0)*nan(0,3)
ans =
0 0 0
0 0 0
The explanation is here.
If performance is the goal, then adding 1 to an all-zero matrix would typically be slower than initializing to all 1.
Initializing to all 1 can be done with a memset() type of function, which in turn could be turned into SIMD hardware instructions for efficiency that only has to write blocks without reading them.
Adding 1 to an all-zero matrix invokes generalized addition, which has to read, add, write.
"Yes, and an other reason is that the name of these functions implies that you want to create a matrix filled with zeroes or ones, not an empty matrix. If the name of functions was not important and only the result was important, the reason for the creation of the function ones() could have been discarded because zeroes() existed and after the creation of the array, you could have just added "1" the the matrix, filling it with ones."
"Syntactic sugar" -- there are many special-purpose functions in Matlab introduced solely for the purpose of simplifying code; just as there are an almost infinite number of ways to fill an array with a value if one doesn't care to use the obvious for some reason. If you don't like the name for a particular purpose, then build yourself a utility function of whatever name you would prefer and use it instead.

Sign in to comment.

 Accepted Answer

Contacting Technical Support using the Contact Us link in the upper-right corner of this page is the way to officially file an enhancement request.
If you want to report an error or request additional information be added to a documentation page, you could press the No button next to the "Was this topic helpful?" question at the end of a documentation page instead of contacting Support.
But now about the enhancement request you want to file, you can create empty cell arrays, struct arrays, or object arrays in several different ways.
C = cell(0, 5) % builds a 0-by-5 cell array
C2 = reshape({}, 0, 5) % Another way to build a 0-by-5 cell array
S = struct('field1', C) % 0-by-5 struct array with one field named field1
S2 = repmat(struct, 0, 5) % 0-by-5 struct array with no fields
S3 = repmat(struct('field2', 5), 0, 5) % 0-by-5 struct array with
% one field named field2
For objects, use the empty method.

More Answers (1)

Hi Philippe
There is a very clean way to create an empty array of any class in Matlab. Each class (including your own user-defined ones, unless you override the behavior) has a static method empty for this purpose. The following example commands will create empty arrays of the respective kind (class) and size:
cell.empty(15, 0)
double.empty(0, 5, 6)
char.empty(1, 0)
Best Jaromir

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!