Although matrices are two dimensional, they do not always appear to have a rectangular shape. A 1-by-8 matrix, for example, has two dimensions yet is linear. These matrices are described in the following sections:
An empty matrix has one or more dimensions
that are equal to zero. A two-dimensional matrix with both dimensions
equal to zero appears in the MATLAB^{®} application as []
.
The expression A = []
assigns a 0-by-0 empty matrix
to A
.
A scalar is 1-by-1 and appears in MATLAB as
a single real or complex number (e.g., 7
, 583.62
, -3.51
, 5.46097e-14
, 83+4i
).
A vector is 1-by-n or n-by-1, and appears in MATLAB as a row or column of real or complex numbers:
Column Vector Row Vector 53.2 53.2 87.39 4-12i 43.9 87.39 4-12i 43.9
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:
A = []; whos A Name Size Bytes Class Attributes A 0x0 0 double
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
A = zeros(0,5) A = 0×5 empty double 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
C = [A B]
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
, 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:
[1 2 3] + ones(0,3) Error using + Matrix dimensions must agree.
Common Operations. The following operations return zero on an empty array:
A = []; size(A), length(A), numel(A), any(A), sum(A)
These operations return a nonzero value on an empty array :
A = []; ndims(A), isnumeric(A), isreal(A), isfloat(A), isempty(A), ... all(A), prod(A)
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:
x = ones(0,3); y = x; y == x ans = 0×3 empty logical array
MATLAB has two distinct types of logical operators:
Short-circuit (&&
, ||
)
— Used in testing multiple logical conditions (e.g., x
>= 50 && x < 100
) where each condition evaluates
to a scalar true
or false
.
Element-wise (&
, |
)
— 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:
true || [] ans = logical 1 false && [] ans = logical 0
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:
true | [] ans = 0×0 empty logical array
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. |
Any individual real or complex number is represented in MATLAB as a 1-by-1 matrix called a scalar value:
A = 5; ndims(A) % Check number of dimensions in A ans = 2 size(A) % Check value of row and column dimensions ans = 1 1
Use the isscalar
function
to tell if a variable holds a scalar value:
isscalar(A) ans = logical 1
Matrices with one dimension equal to one and the other greater than one are called vectors. Here is an example of a numeric vector:
A = [5.73 2-4i 9/7 25e3 .046 sqrt(32) 8j]; size(A) % Check value of row and column dimensions ans = 1 7
You can construct a vector out of other vectors, as long as the critical dimensions agree. All components of a row vector must be scalars or other row vectors. Similarly, all components of a column vector must be scalars or other column vectors:
A = [29 43 77 9 21]; B = [0 46 11]; C = [A 5 ones(1,3) B] C = 29 43 77 9 21 5 1 1 1 0 46 11
Concatenating an empty matrix to a vector has no effect on the resulting vector. The empty matrix is ignored in this case:
A = [5.36; 7.01; []; 9.44] A = 5.3600 7.0100 9.4400
Use the isvector
function
to tell if a variable holds a vector:
isvector(A) ans = logical 1