Getting different colors for different numbers using `spy` in Matlab

matlab, sparse-matrix

Solution

Here is how you can do it:

spy(a,'k')
hold on
spy(a==10,'r')
spy(a==9,'b')
hold off

Another way is to use `scatter` instead of `spy` :

[x,y] = find(a);
clr = a(a~=0);
scatter(x,y,[],clr)
set(gca,'YDir','rev')

In this case the points will be colored by `a` values according to current figure colormap.

Problem

When I use spy to check a sparsity pattern, it doesn't distinguish certain elements from others. Is there any way to do this? Say, for example, elements that are equal to `10` are red and all elements equal to `9` are blue. Can I get this in one `spy` plot? I've only been able to change the size and style of the plot points.

Original source