visualization of huge matrix
Show older comments
I have a huge matrix(772x18900) of 0,1 and NaNs. I want see the distribution. I used imagesc but I see some values are missing(not shown in the figure). For example, here I know there are 54 1s in the first row. But what I see is way lesser. Is there a better way to do this?

Answers (5)
Star Strider
on 18 May 2021
1 vote
Its purpose is to visualise sparse matrices, however it could work with yours as well.
2 Comments
Jalaja Madhusudhanan
on 18 May 2021
One approach may be to do a bit of experimentation.
If ‘M’ is your matrix —
M = randi([0 1], 200, 400);
M(randi(8E+4,1, 200)) = NaN;
figure
spy(M>0)
title('Non-Zero Elements')
figure
spy(M==0)
title('Zero Elements')
figure
spy(isnan(M))
title('NaN Elements')
figure
spy(M>0,'.b')
hold on
spy(M==0,'.r')
spy(isnan(M),'.g')
hold off
legend('Non-Zero Elements','Zero Elements','NaN Elements', 'Location','southoutside')
.
Image Analyst
on 19 May 2021
1 vote
If you have a huge matrix with only tiny bits here and there that show up as just a pixel, or not even a pixel if they're too small because they got subsampled away when it came time to display with imshow(), then you can try imdilate(). It's basically a local max filter. So if you use a big window, like 101 by 101, then it will enlarge all those single isolated points up to a 101x101 size and now be big enough to see.
Jan
on 18 May 2021
0 votes
Use imresize to interpolate the matrix to the size of real pixels on the screen.
In the original plot it seems like the interpolation scheme by imagesc let some pixels vanish.
It may be that part of the issue you're having with display is the fact that these view tools do nearest-neighbor interpolation when scaling. As your image geometry is much larger than the display geometry, single-pixel-wide features simply disappear.
You can use bilinear interpolation to display the image, though it will obviously render many features very lightly, as they only occupy a fraction of a screen pixel.
s = 2000;
A = zeros(s,s);
A(randi(s^2,1000,1)) = 1;
A(randi(s^2,1000,1)) = NaN;
imshow(A,'interpolation','bilinear')
Here, I'm using imshow(), but image() and imagesc() both support the same option. The only issue with using those is the fact that most colorful colormaps are ill-suited for the task and cannot render the image with sufficient contrast for anything to be visible. You could try using
colormap(gray)
or something else if you want. After all, you're only really after the first and last color in the map.
You could also try prescaling the image and either stretching the contrast or thresholding the result so that fractional pixels show up more clearly:
B = double(imresize(A,[NaN 1000],'bilinear')>1E-6);
imshow(B,'interpolation','bilinear')
3 Comments
Jalaja Madhusudhanan
on 18 May 2021
Walter Roberson
on 18 May 2021
The 'interpolation' option is new as of R2019b.
DGM
on 18 May 2021
Oof. I didn't even notice that.
Walter Roberson
on 18 May 2021
0 votes
See https://www.mathworks.com/matlabcentral/answers/822370-resizing-a-binary-image-issue#answer_692825 for example code that makes sparse values more visible in reduced images.
The code does not, however, deal with NaN. It is not immediately clear what you would want the result to be in a block that contains both non-zero values and nan: does the "hole" from the NaN take priority over the non-zero value? If you have a subblock that contains only 0 and nan, should that become a "hole", or does there have to be a minimum threshold of nan before it becomes a hole ?
13 Comments
Jalaja Madhusudhanan
on 18 May 2021
Walter Roberson
on 18 May 2021
Then adapt the code I posted before. The default for max() is to ignore nan unless all of the input is nan, so it should be okay.
Jalaja Madhusudhanan
on 19 May 2021
Image Analyst
on 19 May 2021
@Jalaja Madhusudhanan, but what happened when you tried my suggestion of imdilate()? Did you ignore it?
Jalaja Madhusudhanan
on 20 May 2021
Edited: Walter Roberson
on 20 May 2021
Walter Roberson
on 20 May 2021
To get rid of the warning use
imshow(BW2, 'InitialMagnification', 'fit')
In the post I linked to, the user specifically wanted 142x128 . I do not know why.
%get some data for demonstration purposes
BW = im2bw(imread('flamingos.jpg'));
figure
imshow(BW)
Omit the above steps in practice; they are here just to demonstrate the code.
%do the work
%best-fit calculation
figure
ax = gca;
h = imshow(BW);
ax.Units = 'Pixels';
pos = ax.InnerPosition;
wid = pos(3)
hi = pos(4)
rb = floor(size(BW,1)/hi) + 1
cb = floor(size(BW,2)/wid) + 1
BW2 = cast(blockproc(BW, [rb, cb], @(block) max(block.data(:))), class(BW));
h.CData = BW2;
This code does not imresize(). It divides the image up into blocks and summarizes each block by its maximum. In the above example, it divides into 2 x 2 blocks; this has the effect that the summarized image is 1/2 the original size. An image that was larger relative to the drawing area might be reduced proportionally more.
This code uses the full available drawing area. If you are writing the code into a GUI then replace
figure
ax = gca;
with setting ax to the handle of the previously-created axes to draw into.
Jalaja Madhusudhanan
on 21 May 2021
Image Analyst
on 21 May 2021
@Jalaja Madhusudhanan, for example if you were using GUIDE and had two axes on the window names axesImage1 and axesImage2, and you wanted to operate on axesImage1, you'd do delete the call to figure and do this instead
%figure % Don't do this.
ax = handles.axesImage1; % Instead of gca, use the actual name of the axes.
or you can do this;
axes(handles.axesImage1); % Make axesImage1 the current axes.
ax = gca;
Walter Roberson
on 21 May 2021
Image Analyst is correct.
Jalaja Madhusudhanan
on 22 May 2021
Jalaja Madhusudhanan
on 22 May 2021
Walter Roberson
on 22 May 2021
WIth regard to handles: Image Analyst specifically said "if you were using GUIDE" . handles are a programming method used by GUIDE to keep track of graphics objects in GUIs built using GUIDE. If you build a GUI using App Designer then you would use "app" as the prefix instead of "handles" .
But that all relates to the possibility that you have designed a GUI and you want the image to appear in a particular axes in the GUI. It does not matter if you are programming the calls to create new figures yourself.
Your image appears to have text as well as binary values. It is not realistic to expect the text to be readable after reducing the image to fit in the window.
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!











