Working with Large Matrices in MATLAB

MATLAB handles n-by-n matrix visualization fine until the dimensions get large enough that rendering becomes a pain. I have spent too many afternoons dealing with this. The workflow is straightforward: create the matrix, compute whatever metric you need, generate the plot, and export it. But there are a handful of practical issues that nobody bothers documenting clearly. Here is how to actually get from a matrix object to a publication-ready PDF without the output being garbage.

Xnxn Matrix Matlab Plot Pdf Download

I run into this all the time when preparing supplementary material for papers. You need a clean plot of your matrix eigenvalues, your condition number surface, or just a heat map of the entries. Here is the code I use as a starting point every single time: eigvals = eig(A); figure('Position', [100 100 800 600]); stem(eigvals, 'Marker', 'none', 'LineWidth', 1.2); xlabel('Eigenvalue Index'); ylabel('Magnitude'); set(gca, 'FontSize', 11, 'TickLabelInterpreter', 'latex'); pdfname = 'matrix_plot.pdf'; print(gcf, pdfname, '-dpdf', '-r300'); The `-r300` flag matters. Without it, you get resolution that looks like a screenshot from 2005, and reviewers will notice. The `TickLabelInterpreter` set to `latex` means your axis labels actually render properly instead of showing MATLAB's default math font, which looks lazy in any PDF meant for publication.

One problem most people hit: when your matrix exceeds roughly 5000-by-5000, the `eig` function starts consuming massive amounts of RAM and the stem plot slows to a crawl because it is trying to render thousands of individual markers. I worked around this on a recent project with a 12000-by-12000 sparse matrix by switching to `eigs` for the exterior eigenvalues and plotting only the top 200, which took the computation from about 40 minutes down to roughly 3 minutes on the same machine. Another thing nobody mentions: if your matrix contains NaN or Inf values, MATLAB's `print` command will silently drop the entire figure frame and produce a PDF with blank pages. I spent a full day chasing this on a simulation output where a boundary condition produced a single Inf in a 2000x2000 matrix. The fix was wrapping the plotting in a check before rendering: if any(isnan(A(:))) || any(isinf(A(:))) warning('Matrix contains NaN/Inf values — replacing with nearest finite entry'); A(isnan(A)) = 0; A(isinf(A)) = max(A, [], 'all'); end

Get the Full Details

XNXN Matrix Matlab Plot PDF
XNXN Matrix Matlab Plot PDF

This is destructive to the data, obviously, so be careful. But it beats losing a plot in a PDF right before submission. When exporting, `print` is the standard route. There is also the export options approach that gives you more control over line widths and fonts: exportoptions = graphicsExportOptions('pdf'); exportoptions.FontSize = 12; exportoptions.LineWidth = 1.5; exportgraphics(gcf, pdfname, exportoptions);

Use `exportgraphics` if you are on R2020a or later. It produces cleaner output than `print` because it does not try to re-render the figure at screen resolution first. It writes the vector graphics directly. Some limitations worth knowing. MATLAB PDFs are vector-based, which is good for quality but bad if your plot has tens of thousands of points. A scatter plot with 100,000 data points exported to PDF will create a file that is 15 to 20 megabytes and may not open correctly in older PDF readers. In that case, switch to a raster export at 600 DPI or consider using Python's matplotlib for the final export, which handles large datasets more gracefully in vector format. Also, if you are generating plots in a loop for a parameter sweep, do not leave figures open. Close them immediately after printing. I once had a script running overnight that created 300 figures and never closed them. MATLAB ran out of handles around figure 247 and started throwing errors, which meant the last 53 PDFs were simply missing from the output directory. Add a `close()` after every `print` call. It is that simple and it prevents a class of bug that is annoying to debug because MATLAB does not tell you it skipped the plot.