Chapter 2: MATLAB Basic Tutorial: Array and Matrix Operations

Chapter 2: MATLAB Basic Tutorial: Array and Matrix Operations

MATLAB Basic Tutorial: Array and Matrix Operations

In MATLAB, arrays and matrices are important tools for numerical calculations. This tutorial will discuss the operation of arrays and matrices in MATLAB in detail, and provide detailed cases and code examples.

1. Array

An array is one of the most basic data structures in MATLAB, which can store multiple elements of the same type. In MATLAB, common array types include one-dimensional arrays, two-dimensional arrays (matrixes), and multidimensional arrays. Following are some of the main operations related to arrays.

1.1. Creating Arrays

You can use square brackets []to create arrays and add elements to them. For example:

x = [1, 2, 3, 4, 5]; % 创建一个一维数组

result:

x =

     1     2     3     4     5

You can also use the built-in functions zeros(), ones(), , rand()etc. to create arrays of specific types and sizes. For example:

a = zeros(3, 2); % 创建一个3行2列的零数组
b = ones(2, 2); % 创建一个2行2列的全1数组
c = rand(4, 4); % 创建一个4行4列的随机数数组

result:

a =

     0     0
     0     0
     0     0


b =

     1     1
     1     1

c =

    0.8147    0.0975    0.1576    0.1419
    0.9058    0.2785    0.9706    0.4218
    0.1270    0.5469    0.9572    0.9157
    0.9134    0.9575    0.4854    0.7922

1.2. Accessing array elements

To access a specific element in an array, you can use the index operator ()and provide the corresponding index value. Indexes start at 1, not 0. For example:

x = [1, 2, 3, 4, 5];
element = x(3); % 访问第三个元素,结果为3

result:

element = 
   3
A = [1, 2; 3, 4];
value = A(2, 1); % 访问矩阵中第2行第1列的元素,结果为3

result:

value =
   3

1.3. Array operations

MATLAB provides a wealth of array operation functions, which can perform various operations on arrays.

1.3.1. Arithmetic operations

You can perform element-wise operations on arrays using arithmetic operators such as addition, subtraction, multiplication, and division. For example:

a = [1, 2, 3];
b = [4, 5, 6];

c = a + b; % 对两个数组逐元素相加
d = a .* b; % 对两个数组逐元素相乘

e = 2 * a; % 将标量与数组的每个元素相乘
f = a.^2; % 对数组的每个元素进行平方

result:

c =

     5     7     9

d =

     4    10    18

e =

     2     4     6

f =

     1     4     9

1.3.2. Statistical calculation

MATLAB provides many built-in functions for performing statistical operations on arrays, such as sum, mean, variance, etc.

a = [1, 2, 3, 4, 5];

sum_val = sum(a); % 求数组中所有元素的和
mean_val = mean(a); % 求数组中所有元素的均值
std_val = std(a); % 求数组中所有元素的标准差

result:

sum_val =
   15

mean_val =
   3

std_val =
   1.58113883008419

2. Matrix operations

In MATLAB, a matrix is ​​a two-dimensional array of numbers. Matrices have more algebraic and linear algebra operations than 1D arrays.

2.1. Create matrix

You can use semicolons ;or newlines to create matrices and add elements to them. For example:

A = [1, 2, 3; 4, 5, 6; 7, 8, 9]; % 创建一个3x3的矩阵

result:

A =

     1     2     3
     4     5     6
     7     8     9

2.2. Accessing matrix elements

Similar to arrays, to access a specific element in a matrix, use the indexing operator and provide the corresponding row and column index values.

A = [1, 2, 3; 4, 5, 6; 7, 8, 9];
element = A(2, 3); % 访问第2行第3列的元素,结果为6

result:

element =
   6

2.3. Matrix operations

MATLAB provides a variety of matrix operation methods, such as addition, multiplication, transposition, etc.

2.3.1. Addition and subtraction

Matrix addition and subtraction need to ensure that the two matrices have the same size. For example:

A = [1, 2; 3, 4];
B = [5, 6; 7, 8];

C = A + B; % 矩阵加法
D = A - B; % 矩阵减法

result:

C =

     6     8
    10    12

D =

    -4    -4
    -4    -4

2.3.2. Matrix multiplication

Matrix multiplication is represented in MATLAB using *the operator. Note that in order to perform matrix multiplication, the dimensions of the two matrices must conform to the operation rules.

A = [1, 2; 3, 4];
B = [5, 6; 7, 8];    

E = A * B; % 矩阵乘法

result:

E =

    19    22
    43    50

2.3.3. Transpose

The transpose of a matrix can be computed by using 'the or functions.transpose()

A = [1, 2, 3; 4, 5, 6];

B = A.'; % 矩阵转置
C = transpose(A); % 矩阵转置

result:

B =

     1     4
     2     5
     3     6

C =

     1     4
     2     5
     3     6

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132222310