Programming |
A matrix having at least one dimension equal to zero is called an empty matrix. The simplest empty matrix is 0-by-0 in size. Examples of more complex matrices are those of dimension 0-by-5 or 10-by-0.
To create a 0-by-0 matrix, use the square bracket operators with no value specified:
You can create empty matrices (and arrays) of other sizes using the zeros
, ones
, rand
, or eye
functions. To create a 0-by-5 matrix, for example, use
Operating on an Empty Matrix
The basic model for empty matrices is that any operation that is defined for m
-by-n
matrices, and that produces a result whose dimension is some function of m
and n
, should still be allowed when m
or n
is zero. The size of the result of this operation is consistent with the size of the result generated when working with nonempty values, but instead is evaluated at zero.
For example, horizontal concatenation
requires that A
and B
have the same number of rows. So if A
is m
-by-n
and B
is m
-by-p
, then C
is m-
by-(n+p)
. This is still true if m
or n
or p
is zero.
As with all matrices in MATLAB, you must follow the rules concerning compatible dimensions. In the following example, an attempt to add a 1-by-3 matrix to a 0-by-3 empty matrix results in an error:
Using Empty Matrices in Relational Operations
You can use empty matrices in relational operations such as "equal to" (==
) or "greater than" (>
) as long as both operands have the same dimensions, or the nonempty operand is scalar. The result of any relational operation involving an empty matrix is the empty matrix. Even comparing an empty matrix for equality to itself does not return true
, but instead yields an empty matrix:
Using Empty Matrices in Logical Operations
MATLAB has two distinct types of logical operators:
&&
, ||
) -- Used in testing multiple logical conditions (e.g., x >= 50 && x < 100
) where each condition evaluates to a scalar true
or false
.
&
, |
) -- Performs a logical AND, OR, or NOT on each element of a matrix or array.
Short-circuit Operations. The rule for operands used in short-circuit operations is that each operand must be convertible to a logical scalar value. Because of this rule, empty matrices cannot be used in short-circuit logical operations. Such operations return an error.
The only exception is in the case where MATLAB can determine the result of a logical statement without having to evaluate the entire expression. This is true for the following two statements because the result of the entire statements are known by considering just the first term:
Element-wise Operations. Unlike the short-circuit operators, all element-wise operations on empty matrices are considered valid as long as the dimensions of the operands agree, or the nonempty operand is scalar. Element-wise operations on empty matrices always return an empty matrix:
Note This behavior is consistent with the way MATLAB does scalar expansion with binary operators, wherein the nonscalar operand determines the size of the result. |
Empty Matrices, Scalars, and Vectors | Scalars |
© 1994-2005 The MathWorks, Inc.