Skip to content

matlab/matlab-agentic-toolkit / skills-catalog/matlab-software-development/matlab-optimize-memory

matlab-optimize-memory

Verified

Guides the 7-step MATLAB memory optimization workflow: baseline, profile, identify, optimize, measure, verify, report. Use when asked to reduce MATLAB memory usage, find memory bottlenecks, fix out-of-memory errors, or optimize memory-intensive code.

FileKindSize
SKILL.mdskill md5.2 KB
manifest.yamlconfig450 B
references/memory-patterns.mdmarkdown1.6 KB

references/memory-patterns.md

View on GitHub
# Memory Optimization Patterns

MATLAB-specific memory patterns to apply after profiling identifies hotspots (Step 3).

## Cell Collection + vertcat (O(N) vs O(N²))

Growing arrays with `[arr; chunk]` copies the entire array each iteration.

```matlab
% Before: O(N²) reallocation
waveform = [];
for i = 1:numSegments
    waveform = [waveform; generateSegment(i)];
end

% After: O(N) — single allocation at the end
chunks = cell(numSegments, 1);
for i = 1:numSegments
    chunks{i} = generateSegment(i);
end
waveform = vertcat(chunks{:});
```

## Direct Allocation with zeros(...,'like',x)

Replace patterns that create zeros through multiplication.

```matlab
% Before: allocates + multiplies
result = 0 * x;

% After: direct allocation
result = zeros(size(x), 'like', x);
```

## Copy-on-Write Sharing

MATLAB shares memory between variables until one is modified. Exploit for read-only data.

```matlab
% Before: 4 allocations
coeffUpper = eye(N);
coeffLower = eye(N);

% After: shared via COW (1 allocation, shared until modified)
I = eye(N);
coeffUpper = I;
coeffLower = I;
```

## Implicit Expansion Instead of repmat

MATLAB R2016b+ broadcasts dimensions automatically — no need for `repmat`.

```matlab
% Before: explicit replicated array
B = repmat(meanVec, [nRows 1]);
centered = data - B;

% After: implicit expansion — no extra copy
centered = data - meanVec;
```

## max/min Instead of Logical Masking

The pattern `x .* (x > 0)` creates a temporary logical array the same size as `x`.

```matlab
% Before: temporary logical array
y = x .* (x > 0);

% After: no temporary
y = max(x, 0);
```

Copyright 2026 The MathWorks, Inc.
planning-and-task-breakdownaddyosmani/agent-skillsBreaks work into ordered tasks. Use when you have a spec or clear requirements and need to break work into implementable tasks. Use when a task feels too large to start, when you need to estimate scope, or when parallel work is possible.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
screenpipethedotmack/claude-memSearch screen and audio history recorded by Screenpipe and optionally save selected context into claude-mem. Use when the user asks to recall desktop activity, a meeting, or something seen in another app through Screenpipe.Stars: 117kUpdated Claude CodeCodexVerifiedInstall
contact-widgetnexu-io/open-designSelf-contained floating chat widget with welcome screen, social links, meeting button, and message input. Single HTML file, zero dependencies.Stars: 108kUpdated Claude CodeCodexVerifiedInstall
meeting-notesnexu-io/open-designMeeting notes page — title bar with attendees, agenda checklist, decisions block, action items table with owners + dates, and a "next meeting" footer. Use when the brief mentions "meeting notes", "minutes", "1:1 notes", "all-hands recap", or "会议纪要".Stars: 108kUpdated Claude CodeCodexVerifiedInstall