mcpField
M
matlab

matlab

github.com/crazymsn数据与数据库2336 次安装22 周前更新
代码仓库

关于 matlab

面向数值分析脚本编写者的 MATLAB 与 GNU Octave 科学计算技能,涵盖矩阵、数据处理、绘图、统计和仿真。还提供 MATLAB 与兼容度较高的 Octave 的使用及差异指南。

主要功能

  1. 01矩阵与数组运算
  2. 02线性代数与数值方法
  3. 03绘图与可视化
  4. 04数据导入与导出
  5. 05统计与数据分析
  6. 06MATLAB 与 Octave 指南

适用场景

  1. 01分析实验数据
  2. 02运行数值仿真
  3. 03批量处理数据文件
  4. 04绘制信号和科学结果

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/crazymsn/academic-skills --skill matlab

安装后在 Claude Code 中输入/matlab确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
data-import-export.md
executing-scripts.md
graphics-visualization.md
mathematics.md
matrices-arrays.md
octave-compatibility.md
programming.md
python-integration.md

详细介绍

MATLAB/Octave 科学计算

MATLAB 是一种专为矩阵运算和科学计算优化的数值计算环境。GNU Octave 是一个免费的开源替代方案,与 MATLAB 高度兼容。

快速入门

运行 MATLAB 脚本:

# MATLAB (commercial)
matlab -nodisplay -nosplash -r "run('script.m'); exit;"

# GNU Octave (free, open-source)
octave script.m

安装 GNU Octave:

# macOS
brew install octave

# Ubuntu/Debian
sudo apt install octave

# Windows - download from https://octave.org/download

核心功能

1. 矩阵运算

MATLAB 的基础是矩阵和数组:

% Create matrices
A = [1 2 3; 4 5 6; 7 8 9];  % 3x3 matrix
v = 1:10;                     % Row vector 1 to 10
v = linspace(0, 1, 100);      % 100 points from 0 to 1

% Special matrices
I = eye(3);          % Identity matrix
Z = zeros(3, 4);     % 3x4 zero matrix
O = ones(2, 3);      % 2x3 ones matrix
R = rand(3, 3);      % Random uniform
N = randn(3, 3);     % Random normal

% Matrix operations
B = A';              % Transpose
C = A * B;           % Matrix multiplication
D = A .* B;          % Element-wise multiplication
E = A \ b;           % Solve linear system Ax = b
F = inv(A);          % Matrix inverse

完整的矩阵运算内容,请参阅 references/matrices-arrays.md。

2. 线性代数
% Eigenvalues and eigenvectors
[V, D] = eig(A);     % V: eigenvectors, D: diagonal eigenvalues

% Singular value decomposition
[U, S, V] = svd(A);

% Matrix decompositions
[L, U] = lu(A);      % LU decomposition
[Q, R] = qr(A);      % QR decomposition
R = chol(A);         % Cholesky (symmetric positive definite)

% Solve linear systems
x = A \ b;           % Preferred method
x = linsolve(A, b);  % With options
x = inv(A) * b;      % Less efficient

线性代数的详细内容,请参阅 references/mathematics.md。

3. 绘图与可视化
% 2D Plots
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y, 'b-', 'LineWidth', 2);
xlabel('x'); ylabel('sin(x)');
title('Sine Wave');
grid on;

% Multiple plots
hold on;
plot(x, cos(x), 'r--');
legend('sin', 'cos');
hold off;

% 3D Surface
[X, Y] = meshgrid(-2:0.1:2, -2:0.1:2);
Z = X.^2 + Y.^2;
surf(X, Y, Z);
colorbar;

% Save figures
saveas(gcf, 'plot.png');
print('-dpdf', 'plot.pdf');

完整的可视化指南,请参阅 references/graphics-visualization.md。

4. 数据导入/导出
% Read tabular data
T = readtable('data.csv');
M = readmatrix('data.csv');

% Write data
writetable(T, 'output.csv');
writematrix(M, 'output.csv');

% MAT files (MATLAB native)
save('data.mat', 'A', 'B', 'C');  % Save variables
load('data.mat');                   % Load all
S = load('data.mat', 'A');         % Load specific

% Images
img = imread('image.png');
imwrite(img, 'output.jpg');

完整的 I/O 指南,请参阅 references/data-import-export.md。

5. 控制流与函数
% Conditionals
if x > 0
    disp('positive');
elseif x < 0
    disp('negative');
else
    disp('zero');
end

% Loops
for i = 1:10
    disp(i);
end

while x > 0
    x = x - 1;
end

% Functions (in separate .m file or same file)
function y = myfunction(x, n)
    y = x.^n;
end

% Anonymous functions
f = @(x) x.^2 + 2*x + 1;
result = f(5);  % 36

完整的编程指南,请参阅 references/programming.md。

6. 统计与数据分析
% Descriptive statistics
m = mean(data);
s = std(data);
v = var(data);
med = median(data);
[minVal, minIdx] = min(data);
[maxVal, maxIdx] = max(data);

% Correlation
R = corrcoef(X, Y);
C = cov(X, Y);

% Linear regression
p = polyfit(x, y, 1);  % Linear fit
y_fit = polyval(p, x);

% Moving statistics
y_smooth = movmean(y, 5);  % 5-point moving average

统计参考资料,请参阅 references/mathematics.md。

7. 微分方程
% ODE solving
% dy/dt = -2y, y(0) = 1
f = @(t, y) -2*y;
[t, y] = ode45(f, [0 5], 1);
plot(t, y);

% Higher-order: y'' + 2y' + y = 0
% Convert to system: y1' = y2, y2' = -2*y2 - y1
f = @(t, y) [y(2); -2*y(2) - y(1)];
[t, y] = ode45(f, [0 10], [1; 0]);

ODE 求解器指南,请参阅 references/mathematics.md。

8. 信号处理
% FFT
Y = fft(signal);
f = (0:length(Y)-1) * fs / length(Y);
plot(f, abs(Y));

% Filtering
b = fir1(50, 0.3);           % FIR filter design
y_filtered = filter(b, 1, signal);

% Convolution
y = conv(x, h, 'same');

信号处理内容,请参阅 references/mathematics.md。

常见模式

模式 1:数据分析流程
% Load data
data = readtable('experiment.csv');

% Clean data
data = rmmissing(data);  % Remove missing values

% Analyze
grouped = groupsummary(data, 'Category', 'mean', 'Value');

% Visualize
figure;
bar(grouped.Category, grouped.mean_Value);
xlabel('Category'); ylabel('Mean Value');
title('Results by Category');

% Save
writetable(grouped, 'results.csv');
saveas(gcf, 'results.png');
模式 2:数值模拟
% Parameters
L = 1; N = 100; T = 10; dt = 0.01;
x = linspace(0, L, N);
dx = x(2) - x(1);

% Initial condition
u = sin(pi * x);

% Time stepping (heat equation)
for t = 0:dt:T
    u_new = u;
    for i = 2:N-1
        u_new(i) = u(i) + dt/(dx^2) * (u(i+1) - 2*u(i) + u(i-1));
    end
    u = u_new;
end

plot(x, u);
模式 3:批处理
% Process multiple files
files = dir('data/*.csv');
results = cell(length(files), 1);

for i = 1:length(files)
    data = readtable(fullfile(files(i).folder, files(i).name));
    results{i} = analyze(data);  % Custom analysis function
end

% Combine results
all_results = vertcat(results{:});

参考文件

GNU Octave 兼容性

GNU Octave 与 MATLAB 高度兼容。大多数脚本无需修改即可运行。主要差异:

  • 注释可使用 # 或 %(MATLAB 仅支持 %)
  • Octave 支持 ++、--、+= 运算符
  • Octave 中无法使用某些工具箱函数
  • 使用 pkg load 加载 Octave 软件包

完整的兼容性指南,请参阅 references/octave-compatibility.md。

最佳实践

  1. 向量化运算 - 尽可能避免使用循环:

    % Slow
    for i = 1:1000
        y(i) = sin(x(i));
    end
    
    % Fast
    y = sin(x);
    
  2. 预分配数组 - 避免在循环中扩展数组:

    % Slow
    for i = 1:1000
        y(i) = i^2;
    end
    
    % Fast
    y = zeros(1, 1000);
    for i = 1:1000
        y(i) = i^2;
    end
    
  3. 使用适当的数据类型 - 混合数据使用表格,数值数据使用矩阵:

    % Numeric data
    M = readmatrix('numbers.csv');
    
    % Mixed data with headers
    T = readtable('mixed.csv');
    
  4. 添加注释和文档 - 使用函数帮助:

    function y = myfunction(x)
    %MYFUNCTION Brief description
    %   Y = MYFUNCTION(X) detailed description
    %
    %   Example:
    %       y = myfunction(5);
        y = x.^2;
    end
    

其他资源

评论

支持 Markdown