visualization of huge matrix

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?

2 Comments

Try pcolor with colorbar
I tried pcolor. It gave me a black empty plot.
Then I used this:
h = pcolor(X); set(h, 'EdgeColor', 'none')
Then this was the result. Doesn't look right. Atleast what I would like to see is a distribution of filled squares or dots.

Sign in to comment.

Answers (5)

Star Strider
Star Strider on 18 May 2021
See if the spy function will do what you want.
Its purpose is to visualise sparse matrices, however it could work with yours as well.

2 Comments

Sounds ineresting. This is what I see in my case.
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')
.

Sign in to comment.

Image Analyst
Image Analyst on 19 May 2021
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
Jan on 18 May 2021
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.
DGM
DGM on 18 May 2021
Edited: DGM on 18 May 2021
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

I get the following error message:
Error using images.internal.imageDisplayParsePVPairs (line 71)
The parameter, interpolation, is not recognized by
imageDisplayParsePVPairs
Error in images.internal.imageDisplayParseInputs (line 69)
[common_args,specific_args] =
images.internal.imageDisplayParsePVPairs(varargin{:});
Error in imshow (line 241)
images.internal.imageDisplayParseInputs({'Parent','Border','Reduce'},preparsed_varargin{:});
Please help
The 'interpolation' option is new as of R2019b.
Oof. I didn't even notice that.

Sign in to comment.

Walter Roberson
Walter Roberson on 18 May 2021
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

NaN is used to make all of the tracks same size. Otherwise, it is either zero or one and I care only about the non-zero value(1s). Blocks with zero and 1s are holes.
Hope I answered your question
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.
This was the result when I tried imresize with 142x128 size factor like in your code. I don't get by factor I should resize it. It doesn't seem to show the hidden data points.
For example When I used
y=imresize(BW,101);imshow(y)
Here I even reduced the size of the matrix by excluding all rows with no 1s. y is a simplified matrix with 1 and zeros. And I know there is atleast one 1 in the last row, which is not yet seen.
@Jalaja Madhusudhanan, but what happened when you tried my suggestion of imdilate()? Did you ignore it?
It also doesn't help. Not sure if I am using it correct
I tried
SE = strel('square',3);BW2 = imdilate(BW,SE);imshow(BW2)
. It gives a warning that the image is too large and gives more or less the same output below.
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)
wid = 1296
hi = pos(4)
hi = 972
rb = floor(size(BW,1)/hi) + 1
rb = 2
cb = floor(size(BW,2)/wid) + 1
cb = 2
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.
I didn't understand the last part of your comment ''with setting ax to the handle of the previously-created axes to draw into.'' -Can you please explain this.
Thank you very much!
@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;
Image Analyst is correct.
What is 'handles' here?
I get the following error now:Undefined variable "handles" or class "handles.axesImage1".
This is what happened when I used the code previously mentioned without changing anything(figure
ax = gca;)
BW=
BW2=
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.

Sign in to comment.

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Asked:

on 18 May 2021

Commented:

on 22 May 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!