Matlab

zLanqing/codex-claude-academic-skills/scientific-toolkit-skill/references/scientific-skills/matlab

作者 zLanqing7ed6377f0efb6a38951b48ef03b19d996e454b1fFor MATLAB (https://www.mathworks.com/pricing-licensing.html) and for Octave (GNU General Public License version 3)收錄於 2026年10月9日更新於 2026年10月9日

MATLAB and GNU Octave numerical computing for matrix operations, data analysis, visualization, and scientific computing. Use when writing MATLAB/Octave scripts for linear algebra, signal processing, image processing, differential equations, optimization, statistics, or creating scientific visualizations. Also use when the user needs help with MATLAB syntax, functions, or wants to convert between MATLAB and Python code. Scripts can be executed with MATLAB or the open-source GNU Octave interpreter.

AI 產生的概覽

指導撰寫 MATLAB 與 GNU Octave 指令碼,用於數值運算、資料分析與科學視覺化。

功能
此技能提供 MATLAB 與 GNU Octave 的參考資料與程式碼範例,涵蓋矩陣與陣列運算、線性代數、統計、微分方程、訊號處理、繪圖以及檔案匯入匯出。內容也涉及控制流程、函式、Python 整合,以及 MATLAB 與 Octave 的相容性差異。它產出的是指引與範例指令碼,本身不執行任何內容。
適用情境
在撰寫或解說 MATLAB、Octave 程式碼,在 MATLAB 與 Python 之間轉換,或需要 MATLAB 語法與函式協助時使用。適合線性代數、模擬、科學繪圖等數值、科學與工程運算工作。
執行需求
執行或測試產生的指令碼需要安裝 MATLAB 或 GNU Octave,但僅產生指令碼則不需要。此技能未附指令碼,只有說明與參考文件。不需要網路存取。

MATLAB/Octave Scientific Computing

MATLAB is a numerical computing environment optimized for matrix operations and scientific computing. GNU Octave is a free, open-source alternative with high MATLAB compatibility.

Quick Start

Running MATLAB scripts:

bash
# MATLAB (commercial)matlab -nodisplay -nosplash -r "run('script.m'); exit;"
# GNU Octave (free, open-source)octave script.m

Install GNU Octave:

bash
# macOSbrew install octave
# Ubuntu/Debiansudo apt install octave
# Windows - download from https://octave.org/download

Core Capabilities

1. Matrix Operations

MATLAB operates fundamentally on matrices and arrays:

matlab
% Create matricesA = [1 2 3; 4 5 6; 7 8 9];  % 3x3 matrixv = 1:10;                     % Row vector 1 to 10v = linspace(0, 1, 100);      % 100 points from 0 to 1
% Special matricesI = eye(3);          % Identity matrixZ = zeros(3, 4);     % 3x4 zero matrixO = ones(2, 3);      % 2x3 ones matrixR = rand(3, 3);      % Random uniformN = randn(3, 3);     % Random normal
% Matrix operationsB = A';              % TransposeC = A * B;           % Matrix multiplicationD = A .* B;          % Element-wise multiplicationE = A \ b;           % Solve linear system Ax = bF = inv(A);          % Matrix inverse

For complete matrix operations, see references/matrices-arrays.md [blocked].

2. Linear Algebra

matlab
% 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 decompositionR = chol(A);         % Cholesky (symmetric positive definite)
% Solve linear systemsx = A \ b;           % Preferred methodx = linsolve(A, b);  % With optionsx = inv(A) * b;      % Less efficient

For comprehensive linear algebra, see references/mathematics.md [blocked].

3. Plotting and Visualization

matlab
% 2D Plotsx = 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 plotshold 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 figuressaveas(gcf, 'plot.png');print('-dpdf', 'plot.pdf');

For complete visualization guide, see references/graphics-visualization.md [blocked].

4. Data Import/Export

matlab
% Read tabular dataT = readtable('data.csv');M = readmatrix('data.csv');
% Write datawritetable(T, 'output.csv');writematrix(M, 'output.csv');
% MAT files (MATLAB native)save('data.mat', 'A', 'B', 'C');  % Save variablesload('data.mat');                   % Load allS = load('data.mat', 'A');         % Load specific
% Imagesimg = imread('image.png');imwrite(img, 'output.jpg');

For complete I/O guide, see references/data-import-export.md [blocked].

5. Control Flow and Functions

matlab
% Conditionalsif x > 0    disp('positive');elseif x < 0    disp('negative');else    disp('zero');end
% Loopsfor 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 functionsf = @(x) x.^2 + 2*x + 1;result = f(5);  % 36

For complete programming guide, see references/programming.md [blocked].

6. Statistics and Data Analysis

matlab
% Descriptive statisticsm = mean(data);s = std(data);v = var(data);med = median(data);[minVal, minIdx] = min(data);[maxVal, maxIdx] = max(data);
% CorrelationR = corrcoef(X, Y);C = cov(X, Y);
% Linear regressionp = polyfit(x, y, 1);  % Linear fity_fit = polyval(p, x);
% Moving statisticsy_smooth = movmean(y, 5);  % 5-point moving average

For statistics reference, see references/mathematics.md [blocked].

7. Differential Equations

matlab
% ODE solving% dy/dt = -2y, y(0) = 1f = @(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 - y1f = @(t, y) [y(2); -2*y(2) - y(1)];[t, y] = ode45(f, [0 10], [1; 0]);

For ODE solvers guide, see references/mathematics.md [blocked].

8. Signal Processing

matlab
% FFTY = fft(signal);f = (0:length(Y)-1) * fs / length(Y);plot(f, abs(Y));
% Filteringb = fir1(50, 0.3);           % FIR filter designy_filtered = filter(b, 1, signal);
% Convolutiony = conv(x, h, 'same');

For signal processing, see references/mathematics.md [blocked].

Common Patterns

Pattern 1: Data Analysis Pipeline

matlab
% Load datadata = readtable('experiment.csv');
% Clean datadata = rmmissing(data);  % Remove missing values
% Analyzegrouped = groupsummary(data, 'Category', 'mean', 'Value');
% Visualizefigure;bar(grouped.Category, grouped.mean_Value);xlabel('Category'); ylabel('Mean Value');title('Results by Category');
% Savewritetable(grouped, 'results.csv');saveas(gcf, 'results.png');

Pattern 2: Numerical Simulation

matlab
% ParametersL = 1; N = 100; T = 10; dt = 0.01;x = linspace(0, L, N);dx = x(2) - x(1);
% Initial conditionu = 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);

Pattern 3: Batch Processing

matlab
% Process multiple filesfiles = 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 functionend
% Combine resultsall_results = vertcat(results{:});

Reference Files

  • matrices-arrays.md [blocked] - Matrix creation, indexing, manipulation, and operations
  • mathematics.md [blocked] - Linear algebra, calculus, ODEs, optimization, statistics
  • graphics-visualization.md [blocked] - 2D/3D plotting, customization, export
  • data-import-export.md [blocked] - File I/O, tables, data formats
  • programming.md [blocked] - Functions, scripts, control flow, OOP
  • python-integration.md [blocked] - Calling Python from MATLAB and vice versa
  • octave-compatibility.md [blocked] - Differences between MATLAB and GNU Octave
  • executing-scripts.md [blocked] - Executing generated scripts and for testing

GNU Octave Compatibility

GNU Octave is highly compatible with MATLAB. Most scripts work without modification. Key differences:

  • Use # or % for comments (MATLAB only %)
  • Octave allows ++, --, += operators
  • Some toolbox functions unavailable in Octave
  • Use pkg load for Octave packages

For complete compatibility guide, see references/octave-compatibility.md [blocked].

Best Practices

  1. Vectorize operations - Avoid loops when possible:

    matlab
    % Slowfor i = 1:1000    y(i) = sin(x(i));end
    % Fasty = sin(x);
  2. Preallocate arrays - Avoid growing arrays in loops:

    matlab
    % Slowfor i = 1:1000    y(i) = i^2;end
    % Fasty = zeros(1, 1000);for i = 1:1000    y(i) = i^2;end
  3. Use appropriate data types - Tables for mixed data, matrices for numeric:

    matlab
    % Numeric dataM = readmatrix('numbers.csv');
    % Mixed data with headersT = readtable('mixed.csv');
  4. Comment and document - Use function help:

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

Additional Resources

來源與署名

來源:zLanqing/codex-claude-academic-skills位於scientific-toolkit-skill/references/scientific-skills/matlab提交7ed6377

授權條款: For MATLAB (https://www.mathworks.com/pricing-licensing.html) and for Octave (GNU General Public License version 3)

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架