Hello, I have a matrix filled probability numbers (i.e. ranging from 0 to 1) or NaN when the probability is not computed. I would like to display this matrix as a color table (e.g. using imagesc), in order to have a quick visualisation of the result. The colorbar range is thus set as 0 to 1 since I am interested in probability values. However, I would like the NaN fields to appear with another color than the default "-inf" (here the color of 0 since the down limit for the color is set for value 0), for example gray. How can I do this? Thank you very much, Gaelle
Do you need to use imagesc, or are you open to using pcolor? The latter will leave NaNs blank, so they will appear the same color as the axis background.
data = rand(10); data(data > 0.9) = NaN; [nr,nc] = size(data);
subplot(2,1,1); imagesc(data); subplot(2,1,2); pcolor([data nan(nr,1); nan(1,nc+1)]); shading flat; set(gca, 'ydir', 'reverse');
(The NaN-padding in the pcolor call is so the last row and column are shown, similar to imagesc)
You can use the isnan function to find the indices of all NaNs, then set those elements to another value:
>>A = [4 NaN 3;NaN 2 1]; >>A(isnan(A)) = 255
A = 4 255 3 255 2 1
Hi, Thank you for your answer, but this will not work in my case, because I am setting the limit of the colorbar (using caxis([0,1]) ). If I set, as you proposed, the NaN fields as e.g. 255, the color assigned to them will be equivalent to the color assigned to the value 1 (here the upper bound of the colorbar). I would rather code something that assign a specific color (grey) in the case of a NaN field. Is there any way to do this? Gaelle
Hi, No, the aim is still to plot the probability values. For more clarity: this matrix M is a square matrix, where each element (i,j) corresponds to a comparison (a probability calculus) between an element i and j, e.g. the element M(1,2) gives the probability value of the comparison between element 1 and 2 (note that in mx case M(1,2)≠M(2,1) ).
As for the NaN, they are here simply because I want to keep the size of the matrix similar across all calculation and display. They appear because some comparison do not arise. For example, if the jth element does not exist, the entire line j and row j will be set as NaN. For example, if I want to compare 4 elements, but the 2nd one does not exist yet, the matrix will look like:
if true % code 0.3 NaN 0.4 0.1 NaN NaN NaN NaN 0.2 NaN 0.2 0.5 0.5 NaN 0.6 0.4 end
I thus want to display this matrix, but setting the NaN values as something specific (e.g. a grey value so that it resembles the color of the figure background). Is this more clear? Gaelle
What you showed me is almost that. I just would have liked to keep the 'jet' map instead of turning every value into a grey scale. But I should manage to find a trick. Thank you, Gaelle
But one thing which is bad using your way: the colorbar scale is not set according to the values of the probabilities. So actually, I cannot use your trick...
Hi, I am open to use pcolor, it just seemed more complicated to use than imagesc. I will try to implement it. Thank you!
If using pcolor, NaNs are set to the axis background color. So you can set your colormap to color non-NaNs, and set the axis background color to set the color for NaNs. For example, to set NaNs black you can use: set(gca,'color','k')