how can I custom a mask matrix by giving black color to values equal 1 and transparency to NaN values?

9 views (last 30 days)
I found a mask matrix, and I need to apply black color to values equal 1 and transparency to NaN values. I need a cycle of commands in order to obtain (with the pcolor function) a black mask on a transparent background (NaN values) to be superimposed on a second matrix. Could someone help me please?

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 22 Nov 2021
Edited: Benjamin Kraus on 22 Nov 2021
I'm not 100% sure what you are asking, but if I understand your question correctly I don't think what you are asking can be done easily with pcolor alone.
I think you want to use imagesc instead. The problem is that you cannot set the color of individual faces in the surface object created by the pcolor command, you have to either set all the faces to the same color, or rely on the data, but if you set all the faces to the same color the AlphaData used to control transparency is ignored. You can hack around this by appending a color to the colormap, but it is easier to just use the image object, which doesn't have those restrictions.
You can use imagesc instead of pcolor for both the data and the mask, or use pcolor for the data and imagesc for the mask. The downside is that it will be harder to align the two if you do that.
matrix1 = peaks;
% Create a mask with either 1 or NaN. For the sake of an example, the
% "mask" will be all 1s except for two squares in the middle.
maskmatrix = ones(size(peaks));
maskmatrix(5:20,15:35) = NaN;
maskmatrix(30:45,15:35) = NaN;
% Draw the data using pcolor.
imagesc(matrix1);
% Convert the mask into an [n m 3] image of all zeros (for black).
maskim = repmat(maskmatrix~=1, 1, 1, 3);
hold on
h = imagesc(maskim);
% Set the AlphaData to 0 where there is a NaN in the mask so the NaN values
% are transparent.
h.AlphaData = ~isnan(maskmatrix);
% (optional) Set AlphaData to 0.6 (instead of 1) everywhere else so you can see some of
% the data under the mask.
h.AlphaData = ~isnan(maskmatrix)*0.6;
  3 Comments
Benjamin Kraus
Benjamin Kraus on 23 Nov 2021
Edited: Benjamin Kraus on 23 Nov 2021
That was my understanding, and I still think my suggestion above does what you are requesting.
This is not easy to do with pcolor because the pcolor command creates a Surface object. With Surface objects, you have two choices.
  1. You can color all the faces the same solid color (such as black), but if you do that NaN values in the data are ignored.
  2. You can color the faces based on the colormap, which means each value gets a different color pulled from the colormap.
You can get around that limitation in a few ways, including:
  1. Set the colormap to a single value, just black. But that will mean your image will also be all black, so that doesn't help.
  2. You can append a single black color to the end of the colormap, but that requires some trickery to make sure that the black doesn't show up in your image.
Furthermore, there is a flaw with mixing imagesc and pcolor like you did in your code. With imagesc the center of each pixels is at integer values by default. With pcolor the edge of each pixel is at integer values by default, resulting in a 0.5 pixel shift from the image to the surface, so they won't be aligned correctly unless you also specify x and y data for either imagesc or pcolor (or both). In addition, when you provide a 20x20 input matrix to pcolor the resulting picture has only 19x19 pixels (the last row and column are not shown).
My suggestion above uses imagesc and avoids all the issues above, and I'm pretty sure it accomplishes what you are requesting. In my suggested code my matrix1 is your C matrix, and my maskmatrix is your mask_V_NaN.

Sign in to comment.

More Answers (0)

Categories

Find more on Images 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!