Plot the numbers of a matrix as points (dots) in a plot

5 views (last 30 days)
I want to plot the numbers of a matrix as points (dots) in a plot. For example if I have matrix A:
1 0 5 7
3 0 8 4
6 4 5 2
How can I plot it as a whole by having each number of the matrix as a point in the plot?
I mean:
in the position i,j=1,1 where is the number 1 give me 1 dot in the plot at position i,j=1,3 where is number 5 give me 5 dots in the plot at position i,j=3,2 where is number 4 give me 4 dots in the plot and so on. What I want to do is when I have a matrix let’s say 4X3 or 5X8 or in general mXn then I want to create a mXn grid by plot there, any element of the matrix with points. For instance if I have A=[1 2 3 4;5 6 7 8;0 2 3 23] (a 4X3 matrix) how can I create a 4X3 grid in a plot which any number of the matrix appearing in the plot with dots, as an example at the position i,j=1,1 which is the number one in the matrix I want to have 1 dot in the grid ,in the position i,j=3,4 where is the number 23 ,I want to see 23 dots in the grid.

Accepted Answer

Mike Garrity
Mike Garrity on 2 Jun 2015
Here's a really simple, but inelegant solution:
A=[1 0 5 7; 3 0 8 4; 6 4 5 2]
cla
axis ij
hold on
while any(A(:) > 0)
[r,c] = find(A>0);
offx = randn/10;
offy = randn/10;
scatter(c+offx,r+offy,'filled')
A = A-1;
end
The inelegant part is that it doesn't do a good job of laying out the dots relative to each other. I'll leave that as an exercise for the reader, as they say. The problem is that a nice layout depends on the number of dots, so it's hard to do a good job while processing all of the dots together. My personal favorite layout would be Brent Yorgey's factorization diagrams .
  3 Comments
Ang Vas
Ang Vas on 8 Jun 2015
Edited: Walter Roberson on 8 Jun 2015
It was all most what i was looking for. The problem that I have with this, is that I can't take the x,y coordinates of each point. I thought that it might be xcoordinates=c+offx and ycoordinates=r+offy and I tried to convert the code in order to have the coordinates like
A=[1 0 5 7; 3 0 8 4; 6 4 5 2]
%%cla
axis ij
hold on
while any(A(:) > 0)
[r,c] = find(A>0);
offx = randn/10;
offy = randn/10;
scatter(c+offx,r+offy,'filled')
xcoordinates=c+offx
ycoordinates=r+offy
xy=[xcoordinates;ycoordinates]
A = A-1;
end
but I don't get what I was looking for. I expect to took 45 pairs of numbers (xcoordinates,ycoordinates) as is the numbers of the dots from the matrix A but I unfortunately I din't. Any Ideas !!!
Walter Roberson
Walter Roberson on 8 Jun 2015
"A = A - 1" subtracts 1 from every element of A. So the first subtraction would take A to [0 -1 4 6; 2 -1 7 3; 5 3 4 1]. Clearly after a total of 8 iterations you would have [-7 -8 -3 -1; -5 -8 0 -4; -2 -4 -3 -6] and then there would be no entries in A that were positive and the while() would stop.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!