matlab color map nan in either matrix - 3 designated colors

729 Views Asked by At

I have two matrices A and B of the same size. Each can contain NaNs at some places. I visualize some measure C for the difference between A and B in the following way:

C = (A - B) ./ ((A + B) ./ 2) ;

figure(1); clf;

imagesc(C); colorbar;

I'd like to change the current presentation in a way so that:

  1. If isnan(A(i,j)) and ~isnan(B(i,j)) ---> white in the color map
  2. If ~isnan(A(i,j)) and isnan(B(i,j)) ---> black in the color map
  3. If isnan(A(i,j)) and isnan(B(i,j)) ---> gray in the color map
  4. otherwise ---> color according to C(i,j) value [as is now]

The colors above don't really matter as long as it is distinguishable.

I see there are similar ideas (here, for example) but not exactly what I need... Any ideas?

Many thanks!!!

1

There are 1 best solutions below

0
On

Perhaps there are better solutions out there but I can come out with one here. Essentially, I'm creating an RGB representation of the image to show it. So I picked a colormap with the number of colors I wanted at first. Then, from the data, convert them to index and use in2rgb() to get the RGB image. Then, I can get the bin size of each color and label the color bar appropriately.An example plot

% Randomly generate some data for demonstration
A = [1 nan nan; 1 2 3; 4 5 6];
B = [nan 0 nan; 4 3 2; 1 2 1];
C = (A - B) ./ ((A + B) ./ 2);

% Number of colors you want to use
ncolor = 8;
data_colormap = [jet(ncolor); 1 1 1; 0 0 0; 0.5 0.5 0.5];
data_range = [min(C(:)) max(C(:))];
data_ind = (C - data_range(1)) / (data_range(2) - data_range(1)) * (ncolor - 1) + 1;

% Assign indices > ncolor for the special cases
data_ind(isnan(A)) = ncolor + 1;            % isnan(A) assign to white
data_ind(isnan(B)) = ncolor + 2;            % isnan(A) assign to black
data_ind(isnan(A) & isnan(B)) = ncolor + 3; % isnan(A) + isnan(B)

% Get the RGB representation
img = ind2rgb(round(data_ind), data_colormap);
imagesc(img)

% Custom labels for the colorbar
bin_size = (data_range(2)-data_range(1)) / ncolor;
caxis([data_range(1) data_range(2) + 3*bin_size])
colormap(data_colormap)
ax = colorbar;
yticks = get(ax, 'YTick');
yticks = yticks(yticks < data_range(2));
yticklabels = num2cell(yticks);

% Pad another 3 custom labels
yticks = [yticks, data_range(2)+[bin_size 2*bin_size 3*bin_size]-0.5*bin_size];
yticklabels = [yticklabels 'isnan(A)', 'isnan(B)', 'both'];

set(ax, 'YTick', yticks, 'YTickLabel', yticklabels)