I have a 418x284 matrix filled with 0s and 1s and want to plot a graph where the points are all the location of one's and the x and y coordinates are (0 to 284, 0 to -418).
How can I go about doing this?
Thank you for all your help!
How to plot the coordinates of the nonzero elements in a matrix?
909 Views Asked by user2444127 At
2
There are 2 best solutions below
0

You can also use spy()
but you need to adjust the tick labels.
Suppose you have the following matrix:
A = rand(418,284)>.7;
Then:
spy(A)
Adjusting the labels:
yticks = get(gca, 'YTick');
yticks(yticks ~= 0) = -yticks(yticks ~= 0);
set(gca, 'YTickLabel', yticks)
Let
mat
by your binary matrix. You can get the coordinates of the non-zero elements usingfind
:Note that the convention for axes is different between images and plots in Matlab. The above code assumes
I
is the rows index (top to bottom) andJ
is the columns index (left to right).A working example:
Result: