Matlab学习笔记(三)

本文介绍了MATLAB中如何创建数值数组、矩阵以及进行矩阵拼接。MATLAB的所有变量为数组,创建矩阵时可以使用[ ]操作符。矩阵拼接包括水平和垂直拼接,要求相应维度相同。此外,还提到了cat、horzcat、vertcat函数在创建任意维数组中的应用,以及如何复制和生成数列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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]
B =
    12    62    93    -8    22
    16     2    87    43    91
    -4    17   -72    95     6

当使用这个运算符时,列之间用空格或者逗号(comma)分开,行之间用分号(semicolon)分开。所有的行必须有相同的元素数目。

如果一个matrix只有一行或者只有一列,那么就叫做vector,例如:

C = [1, 2, 3]

或者

D = [10; 20; 30]


Creating and Concatenating Matrices

注意当输入符号时,符号要在数值之前。

  7 -2 +5                                 7 - 2 + 5
   ans =                                     ans =
       10                                        10

上面两个是等价的,下面两个是不等价的:

   [7 -2 +5]                              [7 - 2 + 5]
   ans =                                     ans =
        7    -2     5                            10


Specialized Matrix Functions

MATLAB中有许多函数可以创建不同的matrix:

Function

Description

ones

Create a matrix or array of all ones.

zeros

Create a matrix or array of all zeros.

eye

Create a matrix with ones on the diagonal and zeros elsewhere.

accumarray

Distribute elements of an input matrix to specified locations in an output matrix, also allowing for accumulation.

diag

Create a diagonal matrix from a vector.

magic

Create a square matrix with rows, columns, and diagonals that add up to the same number.

rand

Create a matrix or array of uniformly distributed random numbers.

randn

Create a matrix or array of normally distributed random numbers and arrays.

randperm

Create a vector (1-by-n matrix) containing a random permutation of the specified integers.

大多数这些函数返回的矩阵类型都是double,但是可以使用ones,zeros 和eye改变数值类型,这时需要把MATLAB的类型名作为最后一个参数。例如:

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


Concatenating Matrices 

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是不允许这样的:



Matrix Concatenation Functions

Function

Description

cat

Concatenate matrices along the specified dimension

horzcat

Horizontally concatenate matrices

vertcat

Vertically concatenate matrices

repmat

Create a new matrix by replicating and tiling existing matrices

blkdiag

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值