How to get X and Y coordinates from a Sparse Matrix?

Hey all,
I want to get X and Y coordinates of an image, so that I can generate the graph in an Excel File with those coordinates. I used below code to process an binary image:
img = imread(app.SelectImageEditField.Value);
Icomplement = imcomplement(img);
Icomplement = rgb2gray(Icomplement);
BW = imbinarize(Icomplement, 'adaptive');
out2 = bwskel(BW,'MinBranchLength',15);
BW2 = bwperim(out2,8);
Then I used the following code lines to get a digital graph:
M = sparse(BW2);
spy(M);
Now I want to grab the equivalent X and Y coordinates of this sparse matrix so that I can generate the same graph in Excel. Please suggest a solution. Thank you.

 Accepted Answer

KSSV
KSSV on 25 Nov 2020
Edited: KSSV on 25 Nov 2020
If A is your sparse matrices. You can get positions using:
[y,x] = find(A) ; % x, y are the positons/indices of non-zeros
plot(x,y'.r')

7 Comments

The below command is giving me an error:
plot(x,y'.r')
However I tried this:
[y,x] = find(A);
plot(app.UIAxes, x,y);
But it was not able to plot the same graph. Figure 1 is done using spy function and the right side image is plot.
You should be able to find the indices from the matrix.....What is your matrix? If A is an matrix, you have to find the indices from it.
[i,j] = find(A) ;
spy(A)
hold on
plot(i,j,'.r')
This has worked, Thanks a lot! I used below to get a vertical graph:
plot(j,i,'.r')
However, the axis is now in a reverse order as compared to spy function, May I know how to reverse the axis (or to the data itself) to make it look exactly like in spy function?
Use plot(i,j,'.r')
To reverse y-axis
set(gca,'ydir','reverse')
Full script
A=rand(1000,100)>0.98;
[i,j] = find(A) ;
subplot(1,2,1);
plot(j,i,'.r');
set(gca,'YDir','reverse')
axis equal
axis tight
subplot(1,2,2)
spy(A)

Sign in to comment.

More Answers (0)

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!