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.
-
Have you tried changing the colormap? Using something other than the `jet` default might help increase contrast. – Bjoern H Apr 18 '13 at 06:57
-
@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 Apr 18 '13 at 07:05
-
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 Apr 18 '13 at 07:09
-
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 Apr 18 '13 at 09:02
2 Answers
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:
-
Thanks for answer but the result is as above picture, the points position on top of each other. – Fatime Apr 18 '13 at 08:54
-
The number of elements : no of zeros=26516373 ,no of 0,33=8728 ,no of 0.66=21415,no of ones=17200.any idea?I want to display non zero elements. – Fatime Apr 18 '13 at 09:17
-
1Well you're effectively making a 5k * 5k image which is a 25 mega pixel image which is huge! I think you just need to zoom in a lot. Or display it on a huuuge screen... and still zoom in. I would use `'.'` instead of `'*'` and maybe inspect this image in pieces, like one quadrant at a time or something? Or if the nature of the data permits it, downsample. – Dan Apr 18 '13 at 09:25
-
Yes, you are right as the result show that.Thanks again.How can I represent such matrix information? – Fatime Apr 18 '13 at 09:32
-
-
1You need to create a figure legend and add to it at every iteration (i.e. the figure ledgend label is `Vu(g)` at each iteration. Have a read through this article and give it a shot, if you get stuck then post a new question about figure legends: http://www.mathworks.com/help/matlab/creating_plots/controlling-legends.html – Dan Apr 18 '13 at 11:16
-
Or if you have the statistics toolbox then rather use `gscatter` like I mentioned, it will create the legend for you! – Dan Apr 18 '13 at 11:17
-
I've updated the image to reflect your legend code. Thanks. BTW if the answer helped you and you think it deserves it you can give it an upvote by clicking the upward pointing triangle in the top left of the question ;) – Dan Apr 18 '13 at 12:08
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?

- 144
- 11
-
@Thanks, But test it. It's not correct" plot(u(i), stat(i)/max(stat), '*')".By the way the answer of Dan is correct. The problem is size of matrix. – Fatime Apr 18 '13 at 09:34
-
Dear Fatima! if you need to have the count of the values on the y axis, you should just `plot(u(i), stat(i), '*');` I've just normalized the values diving on maximum number of counts `max(stat)` – zina Apr 18 '13 at 14:16