MATLAB has had empty arrays since before I started using the program. When I started, the only size empty array was 0x0. When version 5 was released, empty arrays came along for the N-dimensional ride and got more shapely.
Even relatively simple expressions involving empty arrays cause confusion from time to time, especially in concert with other rules in MATLAB (such as NaN values usually propagate from inputs to outputs). Let's play around a little with some empty arrays to get some insight.
This post on the MATLAB newsgroup motivated me to talk about empty arrays. Here's is an excerpt from the original post:
I knew that Nan+4 = NaN, but why is +4 =  ? Is there more 'black hole behaviour' I should know about?
Dimensions matter in MATLAB and you will get error messages when dimensions don't agree. Early on, we found it was convenient to treat scalars as an exception with operators (e.g., ,.) and to treat them as if they were expanded to the size of the other operand. So
rand(3,3) + pi
ans = 4.10648118878907 4.09875960183274 3.28347899221701 3.29920573526734 3.62696830231263 3.56335393621607 4.11218543535041 3.94187312247859 4.05732817877886
adds the value pi to the random 3x3 just created. It's as if a 3x3 constant array filled with the value pi was created and added elementwise to the other array.
So what does that mean for an empty operand? Its size has at least one 0 value. So MATLAB expands pi in this expression  + pi to be the same size as  (which happens to be 0x0 here). When that happens, MATLAB creates a second empty array, and then adds the two empty arrays. Hence we get
 + pi
ans = 
returing an empty output.
MATLAB applies the scalar expansion rule to operators. So, for example, size(anything+scalar) is size(anything). As a logical but sometimes surprising consequence, empty arrays often (but not always) propagate through calculations. When doesn't that happen? With some functions where mathematically logical analysis demands different results.
ans = 0
ans = 1
So, why is the sum zero and the product 1? Because they are the identity elements (as in group theory) for sum and product respectively. Think about how to start the computation for a sum or a product and how you would initialize the first value. That's the value given before adding or multiplying any of the array elements, hence the values 0 and 1 respectively.
Empty arrays can be N-dimensional, and don't need all dimensions to be 0. However, they still must obey the rules about dimensions needing to agree. There are consequences that may surprise you, but they do follow logically. Here's an example of trying to add two empty arrays, ending up in an error!
a = zeros(0,1) b = zeros(1,0) try a+b catch MEplus fprintf('\n') disp(MEplus.message) end
a = Empty matrix: 0-by-1 b = Empty matrix: 1-by-0 Matrix dimensions must agree.
For more information about empty arrays, check out this page in the documentation.
Got any empty questions (really, questions about empty!)? Or comments? Post them here.
Get the MATLAB code
Published with MATLAB® 7.9
Comments are closed.
24 CommentsOldest to Newest
indx =  for ii = 1 : 3 if something indx = [indx, ii] end endIt appears the Empty Matrix construction of
zeros(1, 0)behaves the same way as the  construction. I reviewed the referenced documentation & I don't think it goes into enough detail on this topic.
randn(3,3) + piThe following examples shows an error message when adding a vector to a matrix
randn(3,3) + [1:3]This can be fixed by creating a matrix from the vector and adding it to the original matrix.
randn(3,3) + ones(3,1)*[1:3]However, this operation is undesirable when large matrices are involved. Is there a way to add matrix to vec, without for loops, that does not incur a huge penalty in terms of memory?
function s=sizex(x);s=size(x);s(end+1:5)=1;This highlights the occasional case of the 0-by-0-by-1-by-... empty being considered somehow superior to other empties. Consider this sequence:
sizex( num2cell( zeros( 3, 1 ) ) ) = [3 1 1 1 1] sizex( num2cell( zeros( 2, 1 ) ) ) = [2 1 1 1 1] sizex( num2cell( zeros( 1, 1 ) ) ) = [1 1 1 1 1] sizex( num2cell( zeros( 0, 1 ) ) ) = [0 0 1 1 1] % expected: [0 1 1 1 1]This num2cell quirk often trips up my code and forces me to insert special-case handling for empties.
clear A = zeros (4,0) ; try A (5) = pi catch disp (lasterr) end S = sparse (A) ; S (4) = pi whos full (S) clear A = zeros (4,0) ; S = sparse (A) ; S (5) = pi whos full (S)Gives the following:
In an assignment A(I) = B, a matrix A cannot be resized. S = (4,1) 3.1416 Name Size Bytes Class Attributes A 4x0 0 double S 4x1 32 double sparse ans = 0 0 0 3.1416 S = (1,2) 3.1416 Name Size Bytes Class Attributes A 4x0 0 double S 4x2 40 double sparse ans = 0 3.1416 0 0 0 0 0 0 >>A(5)=pi gives an error; A(5,:)=pi doesn't, but it causes A to become 5-by-0. That seems reasonable. I suppose. But then what are S(4)=pi and S(5)=pi doing?
i=8; resultado=i; while (i>1) i=i-1; resultado=[resultado i] endThere are other ways as well, for example, updating resultado in the loop before decrementing i. Then the loop would need to be i>=1 as well, I think. (Or i>0.) --Loren
>> cat (1, zeros(1,0), 1) ??? Error using ==> cat CAT arguments dimensions are not consistent. >> [zeros(1,0); 1] ans = 1Is this a bug, a feature, or by design?