Fatime
Fatime

Reputation: 197

Sparse matrix plot matlab

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

Upvotes: 1

Views: 2552

Answers (2)

Dan
Dan

Reputation: 45741

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

Upvotes: 2

zina
zina

Reputation: 144

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?

Upvotes: 0

Related Questions