Documentation

Creating, Concatenating, and Expanding Matrices

The most basic MATLAB® data structure is the matrix. A matrix is a two-dimensional, rectangular array of data elements arranged in rows and columns. The elements can be numbers, logical values (trueorfalse)、日期和时间的字符串,或者其他一些MATLABdata type.

Even a single number is stored as a matrix. For example, a variable containing the value 100 is stored as a 1-by-1 matrix of typedouble.

A = 100; whosA
Name Size Bytes Class Attributes A 1x1 8 double

Constructing a Matrix of Data

If you have a specific set of data, you can arrange the elements in a matrix using square brackets. A single row of data has spaces or commas in between the elements, and a semicolon separates the rows. For example, create a single row of four numeric elements. The size of the resulting matrix is 1-by-4, since it has one row and four columns. A matrix of this shape is often referred to as a row vector.

A = [12 62 93 -8]
A =1×412 62 93 -8
sz = size(A)
sz =1×21 4

Now create a matrix with the same numbers, but arrange them in two rows. This matrix has two rows and two columns.

A = [12 62; 93 -8]
A =2×212 62 93 -8
sz = size(A)
sz =1×22 2

Specialized Matrix Functions

MATLAB has many functions that help create matrices with certain values or a particular structure. For example, thezerosandonesfunctions create matrices of all zeros or all ones. The first and second arguments of these functions are the number of rows and number of columns of the matrix, respectively.

A = zeros(3,2)
A =3×20 0 0 0 0 0
B = ones(2,4)
B =2×41 1 1 1 1 1 1 1

Thediagfunction places the input elements on the diagonal of a matrix. For example, create a row vectorAcontaining four elements. Then, create a 4-by-4 matrix whose diagonal elements are the elements ofA.

A = [12 62 93 -8]; B = diag(A)
B =4×412 0 0 0 0 62 0 0 0 0 93 0 0 0 0 -8

Concatenating Matrices

You can also use square brackets to join existing matrices together. This way of creating a matrix is calledconcatenation. For example, concatenate two row vectors to make an even longer row vector.

A = ones(1,4); B = zeros(1,4); C = [A B]
C =1×81 1 1 1 0 0 0 0

To arrangeAandBas two rows of a matrix, use the semicolon.

D = [A;B]
D =2×41 1 1 1 0 0 0 0

To concatenate two matrices, they must have compatible sizes. In other words, when you concatenate matrices horizontally, they must have the same number of rows. When you concatenate them vertically, they must have the same number of columns. For example, horizontally concatenate two matrices that both have two rows.

A = ones(2,3)
A =2×31 1 1 1 1 1
B = zeros(2,2)
B =2×20 0 0 0
C = [A B]
C =2×51 1 1 0 0 1 1 1 0 0

An alternative way to concatenate matrices is to use concatenation functions such ashorzcat, which horizontally concatenates two compatible input matrices.

D = horzcat(A,B)
D =2×51 1 1 0 0 1 1 1 0 0

Generating a Numeric Sequence

Thecolonis a handy way to create matrices whose elements are sequential and evenly spaced. For example, create a row vector whose elements are the integers from 1 to 10.

A = 1:10
A =1×101 2 3 4 5 6 7 8 9 10

You can use the colon operator to create a sequence of numbers within any range, incremented by one.

A = -2.5:2.5
A =1×6-2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000

To change the value of the sequence increment, specify the increment value in between the starting and ending range values, separated by colons.

A = 0:2:10
A =1×60 2 4 6 8 10

To decrement, use a negative number.

A = 6:-1:0
A =1×76 5 4 3 2 1 0

You can also increment by noninteger values. If an increment value does not evenly partition the specified range, MATLAB automatically ends the sequence at the last value it can reach before exceeding the range.

A = 1:0.2:2.1
A =1×61.0000 1.2000 1.4000 1.6000 1.8000 2.0000

扩大一个矩阵

You can add one or more elements to a matrix by placing them outside of the existing row and column index boundaries. MATLAB automatically pads the matrix with zeros to keep it rectangular. For example, create a 2-by-3 matrix and add an additional row and column to it by inserting an element in the (3,4) position.

A = [10 20 30; 60 70 80]
A =2×310 20 30 60 70 80
A(3,4) = 1
A =3×410 20 30 0 60 70 80 0 0 0 0 1

You can also expand the size by inserting a new matrix outside of the existing index ranges.

A(4:5,5:6) = [2 3; 4 5]
A =5×610 20 30 0 0 0 60 70 80 0 0 0 0 0 0 1 0 0 0 0 0 0 2 3 0 0 0 0 4 5

To expand the size of a matrix repeatedly, such as within aforloop, it's usually best to preallocate space for the largest matrix you anticipate creating. Without preallocation, MATLAB has to allocate memory every time the size increases, slowing down operations. For example, preallocate a matrix that holds up to 10,000 rows and 10,000 columns by initializing its elements to zero.

A = zeros(10000,10000);

If you need to preallocate additional elements later, you can expand it by assigning outside of the matrix index ranges or concatenate another preallocated matrix toA.

Empty Arrays

An empty array in MATLAB is an array with at least one dimension length equal to zero. Empty arrays are useful for representing the concept of "nothing" programmatically. For example, suppose you want to find all elements of a vector that are less than 0, but there are none. Thefindfunction returns an empty vector of indices, indicating that it couldn't find any elements less than 0.

A = [1 2 3 4]; ind = find(A<0)
ind = 1x0 empty double row vector

Many algorithms contain function calls that can return empty arrays. It is often useful to allow empty arrays to flow through these algorithms as function arguments instead of handling them as a special case. If you do need to customize empty array handling, you can check for them using theisemptyfunction.

TF = isempty(ind)
TF =logical1

Related Topics