How can I plot the items of a matrix with a specific coordinates in matlab?

20 views (last 30 days)
Ang Vas
Ang Vas on 14 Jul 2015
Edited: Ang Vas on 21 Jul 2015
Hello everybody. I need your help about a problem that I have in matlab. I need to plot the items of a matrix with a specific coordinates. For instance lets say that I have the matrix
A=[ 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 2 2 2 2 2]
And I want to plot every item of my matrix as a dot in a plot with axis for instance 10x10 units ( or it could be 50x50 this is not my point). I want to divide this in smaller grid and assume my first grid it consist 5 sub-grids which is 2x2 . So I’m saying that my 10x10 axis consists of 25 pixels with the dimension 2x2. How can I plot each item of my matrix in the subgroup of 2x2 pixel with specific coordinates? May be is a bit confusing what I’m asking indeed. For that reason I made a pattern in excel in order to illustrate what I want to do. Each item of my matrix is being corresponded with specific coordinates in the plot, for example the number 1 in first row and fist column of my matrix is actually one dots in the first sub-grid with x,y coordinates (0.5,9.5) respectively ,where the number 4 in forth row and fifth column of my matrix is 4 dots of the corresponded sub-grid and every dot has coordinates as it seems below and so on.
The chart in the excel it look like this
thank you in advanced

Accepted Answer

Abhishek Pandey
Abhishek Pandey on 17 Jul 2015
Hello Ang, I believe the following sample code will help you with the desired result. You might need to add some error conditions and modify it to satisfy other constraints according to your problem statement or make it more general.
% create a sample matrix
M = [repmat(1, 1, 5); repmat(2, 1, 5);repmat(3, 1, 5); repmat(4, 1, 5); repmat(2, 1, 5)];
% create a figure with desired axis labels and grid on
figure, axis([0 10 0 10]);
grid on; hold on;
% get size of sample matrix
[m,n] = size(M);
% double the size of sample matrix for output display
% subtract 0.5 so that the plot points show up at the centre of the grid
m = m*2 - 0.5;
n = n*2 - 0.5;
% iterate through all points in matrix and plot the points
for i = 1:size(M,1)
I = (i-1)*2;
for j = 1:size(M,2)
J = (j-1)*2 + 0.5;
plot(J,m-I,'bd','MarkerFaceColor', 'b');
plot(J+1,m-I-1,'bd','MarkerFaceColor', 'b');
plot(J+1,m-I,'bd','MarkerFaceColor', 'b');
plot(J,m-I-1,'bd','MarkerFaceColor', 'b');
I hope this helps!
- Abhishek

More Answers (0)

Community Treasure Hunt

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

Start Hunting!