MATLAB Built-ins
8 methodsMATLAB 核心内置函数集合。
disp(X)显示变量值,不显示变量名。
Parameters
| Name | Type | Description |
|---|---|---|
| X | any | 待显示的值或数组 |
Returns
无,输出到命令窗口
Example
matlab
disp('Hello, MATLAB')
disp([1 2; 3 4])size(A)返回矩阵各维的大小。
Parameters
| Name | Type | Description |
|---|---|---|
| A | array | 输入数组 |
Returns
向量或标量,各维尺寸
Example
matlab
A = zeros(3, 4);
s = size(A); % [3 4]
[rows, cols] = size(A);length(A)返回最大维的长度,空数组返回 0。
Parameters
| Name | Type | Description |
|---|---|---|
| A | array | 输入数组 |
Returns
标量,最大维长度
Example
matlab
length([1 2 3 4]) % 4
length(zeros(2,7)) % 7zeros(m,n) / ones(m,n)创建全零或全一的 m×n 矩阵。
Parameters
| Name | Type | Description |
|---|---|---|
| m | integer | 行数 |
| n | integer | 列数 |
Returns
m×n 矩阵
Example
matlab
Z = zeros(2, 3);
O = ones(3);
I = eye(4); % 单位阵sum(A, dim)沿指定维度求和,默认对列求和。
Parameters
| Name | Type | Description |
|---|---|---|
| A | array | 输入数组 |
| dim | integer | 维度(可选) |
Returns
标量或向量,求和结果
Example
matlab
sum([1 2 3]) % 6
sum([1 2; 3 4]) % [4 6] (列求和)
sum([1 2; 3 4], 2) % [3; 7] (行求和)plot(X, Y)绘制 X 与 Y 的二维线图。
Parameters
| Name | Type | Description |
|---|---|---|
| X | vector | x 坐标 |
| Y | vector | y 坐标 |
Returns
图形句柄
Example
matlab
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y, 'r--')
xlabel('x'); ylabel('sin(x)')find(X)返回非零元素的线性索引。
Parameters
| Name | Type | Description |
|---|---|---|
| X | array | 输入数组 |
Returns
索引向量
Example
matlab
idx = find([1 0 0 3 0]);
% idx = [1 4]
[i, j] = find([1 0; 0 4]);
% i = [1; 2], j = [1; 2]reshape(A, m, n)将数组重塑为 m×n 形状(元素总数须一致)。
Parameters
| Name | Type | Description |
|---|---|---|
| A | array | 源数组 |
| m | integer | 新行数 |
| n | integer | 新列数 |
Returns
m×n 矩阵
Example
matlab
A = 1:6;
B = reshape(A, 2, 3);
% B = [1 3 5; 2 4 6]