简体   繁体   中英

Plot sparse matrix in matlab

I want to plot a sparse matrix in an imagesc type of style (one color for each pixel, and not symbols a la scatter ). The matrix consists of blobs that are spread ut over a 10000x10000 square. I expect about 100 blobs and each blob being 50x100 pixels. This matrix is so big that it becomes very laggy to zoom in or out or to move around in it to inspect the data. And I still want to keep the resolution. Is there any way to plot a sparse matrix which just plots the blobs and has the "zero-color" of the colormap as a background that does not take any space in memory?

Lets say we have a matrix M that looks like this:

[1, 2,  1, 0;
 0, 1, .4, 0;
 0, 0,  0, 0;
 0, 7,  0, 0]

When I plot it as a sparse matrix

figure; 
imagesc(sparse(M));

It takes the same size as omitting the sparse-command. This is what I want to circumvent.

Instead of treating the matrix as an image, you could plot only its nonzero values. Using scatter (instead of plot ) allows you to have color as a function of value, as in imagesc .

By default scatter leaves the background white, so you have to adjust that. This is done in two steps: make sure scatter 's color scaling assigns the first color of your colormap to value 0; and then manually set the axis' background to that color.

I haven't tested if this takes up less memory, though.

%// Generate example matrix
M = 10000*rand(1000);
M(M>100) = 0;
M = sparse(M); %// example 1000x1000 matrix with ~1% sparsity

%// Do the plot
cmap = jet; %// choose a colormap
s = .5; %// dot size
colormap(cmap); %// use it
[ii, jj, Mnnz] = find(M); %// get nonzero values and its positions
scatter(1,1,s,0) %// make sure the first color corresponds to 0 value.
hold on
scatter(ii,jj,s,Mnnz); %// do the actual plot of the nonzero values
set(gca,'color',cmap(1,:)) %// set axis backgroud to first color
colorbar %// show colorbar

Note axes' orientation may be different from imagesc .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM