Array Creation and Concatenation
Create Numeric Arrays
在MATLAB中,所有的变量都是arrays,并且,默认所有的数值变量都是double类型。例如:
>> A=100
A =
100
>> whos A
Name Size Bytes Class Attributes
A 1x1 8 double
为了创建一个matrix(一个二维,矩形的array),可以使用[ ] operator([ ]只能用于创建matrix,不能用于创建高维array):
B = [12, 62, 93, -8, 22; 16, 2, 87, 43, 91; -4, 17, -72, 95, 6]
当使用这个运算符时,列之间用空格或者逗号(comma)分开,行之间用分号(semicolon)分开。所有的行必须有相同的元素数目。B = 12 62 93 -8 22 16 2 87 43 91 -4 17 -72 95 6
如果一个matrix只有一行或者只有一列,那么就叫做vector,例如:
C = [1, 2, 3]
或者
D = [10; 20; 30]
注意当输入符号时,符号要在数值之前。
7 -2 +5 7 - 2 + 5 ans = ans = 10 10
上面两个是等价的,下面两个是不等价的:
[7 -2 +5] [7 - 2 + 5] ans = ans = 7 -2 5 10
MATLAB中有许多函数可以创建不同的matrix:
Function | Description |
---|---|
Create a matrix or array of all ones. | |
Create a matrix or array of all zeros. | |
Create a matrix with ones on the diagonal and zeros elsewhere. | |
Distribute elements of an input matrix to specified locations in an output matrix, also allowing for accumulation. | |
Create a diagonal matrix from a vector. | |
Create a square matrix with rows, columns, and diagonals that add up to the same number. | |
Create a matrix or array of uniformly distributed random numbers. | |
Create a matrix or array of normally distributed random numbers and arrays. | |
Create a vector (1-by-n matrix) containing a random permutation of the specified integers. |
A = zeros(4, 6, 'uint32') A = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0一些例子:(具体用法可以参考help文档)
A = magic(5) A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
A = [12 62 93 -8 22]; B = diag(A, -1) B = 0 0 0 0 0 0 12 0 0 0 0 0 0 62 0 0 0 0 0 0 93 0 0 0 0 0 0 -8 0 0 0 0 0 0 22 0
Matrix concatenation就是把一个或者多个matrices合并成一个新的matrix。[ ] operator不仅用于matrix创建,也用于matrix concatenation。
表达式 C = [A B] horizontally concatenates matrices A and B.。表达式 C = [A; B] vertically concatenates them。
如下的例子是vertically concatenate:
A = ones(2, 5) * 6; % 2-by-5 matrix of 6's B = rand(3, 5); % 3-by-5 matrix of random values C = [A; B] % Vertically concatenate A and B C = 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 6.0000 0.9501 0.4860 0.4565 0.4447 0.9218 0.2311 0.8913 0.0185 0.6154 0.7382 0.6068 0.7621 0.8214 0.7919 0.1763
Keeping Matrices Rectangular
我们可以使用已知的matrix创建任意类型的matrices或者更高维的array,但是必须是合法的形状。如下图
如果水平(horizon)创建,那么行数必须相同,如果垂直(vertical)创建,那么列数必须相同。
下图显示水平创建的时候必须行数相同:
下图显示垂直创建的时候列数不同,MATLAB是不允许这样的:
Function | Description |
---|---|
Concatenate matrices along the specified dimension | |
Horizontally concatenate matrices | |
Vertically concatenate matrices | |
Create a new matrix by replicating and tiling existing matrices | |
Create a block diagonal matrix from existing matrices |
Concatenating Matrices and Arrays. 可以使用如下三个函数来替代[ ] operator: cat, horzcat, and vertcat. 使用这些函数,可以创建任意维的arrays。
C = cat(1, A, B); % Concatenate along the first dimension C = vertcat(A, B); % Concatenate vertically
Replicating a Matrix. repmat函数是通过复制已存在的matrix来创建新的matrix的。
repmat(M, v, h)
MATALB竖直复制矩阵M v次,水平复制h次:
A = [8 1 6; 3 5 7; 4 9 2] A = 8 1 6 3 5 7 4 9 2 B = repmat(A, 2, 4) B = 8 1 6 8 1 6 8 1 6 8 1 6 3 5 7 3 5 7 3 5 7 3 5 7 4 9 2 4 9 2 4 9 2 4 9 2 8 1 6 8 1 6 8 1 6 8 1 6 3 5 7 3 5 7 3 5 7 3 5 7 4 9 2 4 9 2 4 9 2 4 9 2
Creating a Block Diagonal Matrix. blkdiag函数是沿着对角线方向创建矩阵的:
A = magic(3); B = [-5 -6 -9; -4 -4 -2]; C = eye(2) * 8; D = blkdiag(A, B, C) D = 8 1 6 0 0 0 0 0 3 5 7 0 0 0 0 0 4 9 2 0 0 0 0 0 0 0 0 -5 -6 -9 0 0 0 0 0 -4 -4 -2 0 0 0 0 0 0 0 0 8 0 0 0 0 0 0 0 0 8
Generating a Numeric Sequence
使用colon operator:默认是以1增长的。
A = 10:15 A = 10 11 12 13 14 15
A = -2.5:2.5 A = -2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000
A = 1:6.3 A = 1 2 3 4 5 6注意这里是增长的,下面的例子显然不对:
A = 9:1 A = Empty matrix: 1-by-0可以改变增长的值:
A = 10:5:50 A = 10 15 20 25 30 35 40 45 50
A = 3:0.2:3.8 A = 3.0000 3.2000 3.4000 3.6000 3.8000
A = 9:-1:1 A = 9 8 7 6 5 4 3 2 1