Skip to content

MATLAB - 数组

MATLAB 中最基本的数据结构是数组(array)。从单个数字(1x1 数组)到复杂数据集,每个变量都以多维数组的形式存储。理解如何创建、操作以及选择正确类型的数组是编写高效且有效 MATLAB 代码的关键。

MATLAB 提供了丰富的函数来创建常见类型的数组:

% A 3x4 matrix of all zeros
% 一个 3x4 的全零矩阵
A = zeros(3, 4);
% A 5x5 square matrix of all ones
% 一个 5x5 的全一正方矩阵
B = ones(5);
% A 4x4 identity matrix
% 一个 4x4 的单位矩阵
I = eye(4);
% A 2x5 matrix of uniformly distributed random numbers between 0 and 1
% 一个 2x5 的在 0 到 1 之间均匀分布的随机数矩阵
R_uniform = rand(2, 5);
% A 3x3 matrix of normally distributed random numbers
% 一个 3x3 的正态分布随机数矩阵
R_normal = randn(3);
% A vector with 100 points evenly spaced from 0 to 10
% 一个包含 100 个点、从 0 到 10 等间距分布的向量
V = linspace(0, 10, 100);

数组可以拥有超过两个维度。它们常用于图像数据(高度 x 宽度 x 颜色通道)或表示 3D 网格上的数据。您可以通过扩展现有 2D 数组或使用带有两个以上维度参数的创建函数来创建它们。

% Start with a 2x3 matrix (Page 1)
% 从一个 2x3 矩阵开始(第 1 页)
A = [1 2 3; 4 5 6];
% Add a second 'page' to create a 2x3x2 array
% 添加第二个“页面”以创建 2x3x2 数组
A(:, :, 2) = [7 8 9; 10 11 12];
% Access the element at row 1, col 3, page 2
% 访问第 1 行、第 3 列、第 2 页的元素
element = A(1, 3, 2); % This will be 9
% 这将是 9
% Create a 4x3x2 array of random numbers directly
% 直接创建一个 4x3x2 的随机数数组
B = rand(4, 3, 2);

除了一个长列表之外,这里列出了处理数组最关键的一些函数:

  • 大小和形状:size(A) 返回数组 A 的维度。numel(A) 返回元素的总数。length(A) 返回最长维度的大小。
  • 重塑:reshape(A, new_dims) 在不改变数组元素的情况下改变其维度。例如,reshape(1:6, 2, 3) 创建一个 2x3 矩阵。
  • 排序:sort(V) 对向量进行排序。对于矩阵 M,sort(M, 1) 对每列进行排序,sort(M, 2) 对每行进行排序。
  • 查找元素:find(A > 5) 返回数组 A 中所有大于 5 的元素的线性索引。

对于更复杂或结构化的数据,现代 MATLAB 提供了专门的数据容器,它们通常比基本的数值数组更高效和方便。

为了处理文本,现代 MATLAB 引入了字符串数组(string arrays),使用双引号 ("") 创建。它们比传统的字符数组(单引号 '')更灵活和强大。

% Modern String Array (Recommended)
% 现代字符串数组(推荐)
names_str = ["Alice", "Bob", "Charlie"];
names_str(2) % Accesses the full string "Bob"
% 访问完整的字符串 "Bob"
% Old Character Array (Avoid if possible)
% 旧字符数组(如有可能,请避免)
names_char = ['Alice'; 'Bob '; 'Charlie']; % Requires padding with spaces to be rectangular
% 需要用空格填充以使其成为矩形

单元格数组(cell array)是一种容器,其中每个元素(一个单元格)可以容纳任何类型的数据,包括数字、文本,甚至其他数组。这对于将不同类型的数据组合在一起非常有用。

使用括号 () 来索引单元格本身,使用大括号 {} 来访问单元格内的内容。

% Create a 2x3 cell array
% 创建一个 2x3 的单元格数组
C = {10, 'some text', [1 2; 3 4]; ...
@(x) x^2, "a string", {1; 2}};
% Get the cell at (1, 3) (which is a cell containing a matrix)
% 获取 (1, 3) 处的单元格(这是一个包含矩阵的单元格)
cell_subset = C(1, 3); % Returns a 1x1 cell array
% 返回一个 1x1 的单元格数组
% Get the CONTENT of the cell at (1, 3) (the matrix itself)
% 获取 (1, 3) 处单元格的 内容 (即矩阵本身)
matrix_content = C{1, 3}; % Returns the 2x2 matrix [1 2; 3 4]
% 返回 2x2 矩阵 [1 2; 3 4]

表格(table)是存储混合类型列式数据的最佳选择,就像您在电子表格或数据库表中会发现的那样。每列可以有不同的数据类型,但一列中的所有数据必须是相同类型。表格数据在数据分析中强烈推荐使用。

% Create data for the table
% 为表格创建数据
LastName = ["Smith"; "Jones"; "Williams"];
Age = [38; 29; 45];
Smoker = [true; false; true];
Height = [71; 69; 64];
% Create the table
% 创建表格
patients = table(LastName, Age, Smoker, Height);
disp(patients);
% Access data using dot notation (very convenient)
% 使用点表示法访问数据(非常方便)
mean_age = mean(patients.Age);
fprintf('Mean patient age: %.1f\n', mean_age);
% Access all data for smokers
% 访问所有吸烟者的数据
smoker_data = patients(patients.Smoker, :);
disp(smoker_data);