1
votes

I have a 5000 *5000 sparse matrix with 4 different values. I want to visualise the nonzero elements with 4 different colors such that I can recognise the ratio of this values and the relationships between them,I use imagesc but I can not recognise very well among different values, especially the values with smaller ratio.I think if I use some symboles for each value , it works but I don't know how is it in Matlab. Any suggestion? The result of Dan code is in figure below.

results of about 5000*5000result of 1000*1000 block of 5000*5000 matrix

2
Have you tried changing the colormap? Using something other than the jet default might help increase contrast. - Bjoern H
@BjoernH Thanks,yes I tried different things but the contrast is not good because the matrix is so sparse and it appear as some very small point. I think I must use some symboles like circle or triangle or symbole like + ... but I don't know how to use them in Matlab. - Fatime
Try my answer, it uses stars but if you want circles then try 'o' instead of '*' plus you can fiddle withthe linewidth property of plot if you want them thicker, and use axis([0, 5000, 0, 5000]) to scale it correctly - Dan
If that's the result then it doesn't look very sparse to me. Are you sure it's only non zero elements you don't want to display, maybe the elements are just very small value and you should replace the == 0 in my code to something like ` < 0.001`? - Dan

2 Answers

2
votes

You could reform the matrix to be a set of [X, Y, F] coordinates (re-using my answer from Resampling Matrix and restoring in one single Matrix):

Assuming your matrix is M

[X, Y] = meshgrid(1:size(M,1), 1:size(M,2));
Mf = M(:); %used again later, hence stored
V = [X(:), Y(:), Mf];

get rid of the zero elements

V(Mf == 0, :) = [];

At this point, if you have access to the statistics toolbox you can just go gscatter(V(:,1), V(:,2), V(:,3)) to get the correct plot otherwise continue with the following if you don't have the toolbox:

Find a list of the unique values in M

Vu = unique(V(:,3));

For each such value, plot the points as an xy scatter plot, note hold all makes sure the colour changes each time a new plot is added i.e. each new iteration of the loop

hold all;
for g = 1:length(Vu)
    Vg = V(V(:,3)==Vu(g),:)
    plot(Vg(:,1), Vg(:,2), '*');
    a{g}=num2str(Vu(g));
end
legend(a);

Example M:

M = zeros(1000);
M(200,30) = 7;
M(100, 900) = 10;
M(150, 901) = 13;
M(600, 600) = 13;

Result:

enter image description here

0
votes

Now i can answer the first part of the question. I suppose you need to do something like

sum(histc(A, unique(A)),2)

to count the number of unique values in the matrix.

temp = histc(A, unique(A)) "is a matrix of column histogram counts." So you get the counts of all values of unique(A) as they appear in A columns.

I'm doing stat = sum(temp,2) to get counts of all values of unique(A) in the whole matrix.

Then you can use the code proposed from @Dan to visualize the result.

hold all; 
u=unique(A);
for i = 1:length(stat) 
plot(u(i), stat(i)/max(stat), '*');
end

Please clarify what kind of relationship between the values do you mean?