All MATLAB variables are arrays. So, each numeric variable can contain multiple numbers. You can store related data in one variable by using an array. 所有的变量都是数组

Because arrays are a basic programming tool in the MATLAB language, it’s important to get to know them and the terminology used to describe them.

Pasted image 20250718005356.png

标量 scalar

In MATLAB, a single number, called a scalar, is represented by a 1-by-1 array, meaning the array contains one row and one column.

向量 vector

1. 直接列写

When you separate numbers by using spaces , MATLAB combines the numbers into a row vector, which is an array with one row and multiple columns (1-by-n).

When you separate numbers by using semicolons, MATLAB creates a column vector (n-by-1).

2. :运算符

y=Start : Spaceing : End

知道步长 For long vectors, entering individual numbers is not practical. An alternative, shorthand method for creating evenly spaced vectors is to use the colon operator (:) and specify only the start and end values. the : operator uses a default spacing of 1. However, you can specify a different spacing.

3. linspace

y=linspace(first,last,number_of_elements) 知道数字的个数 If you know the number of elements you want in a vector (instead of the spacing between each element), you can use the linspace function.
Note the use of commas (,) to separate inputs to the linspace function.

向量转置

Transpose x from a row vector to a column vector using the transpose operator '.

注意

Both linspace and the : operator create row vectors. 都产生行向量 如果要生成列向量,可以转置 x=x'

x=[3 5]  
x=[3;5]
 
y=20 : 2 :26   determine the size of an existing matrix
rand(size(x))   x=2
y=data(:,2)  %% 提取所有行的第二列
 
 

特殊关键字 end 行或列的最后一个索引

use only one index with a matrix, MATLAB traverses down each column in order. 如果对一个矩阵只给一个参数进行索引,则从左到右遍历列