How do I plot the nearest neighbour algorithm in a matrix?

15 views (last 30 days)
Hi,
X = [0 0 0 0 0; 0 0 1 0 0; 0 0 0 0 1]
I would like to produce a coloured grid that displays the matrix elements equal to one as red. Yellow for the elements at a neighbourhood of 8 connectivity to the non-zero elements and green for the elements further away from 1.
Should I be using brute force or knnsearch or is there a better/ faster way to go about it?
Thanks
  1 Comment
Walter Roberson
Walter Roberson on 27 Mar 2017
.... Then don't use that code.
You have forgotten that chessboard distance does not stop at distance 1 or 2.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Mar 2017
Edited: Walter Roberson on 18 Mar 2017
d = bwdist(X);
t = ones(size(X));
t(d < 1.5) = 3;
t(d == 0) = 2;
cmap = [0 1 0;
1 0 0;
1 1 0];
image(t);
axis image
colormap(cmap)

More Answers (1)

Chad Greene
Chad Greene on 18 Mar 2017
How about bwlabel? Then you can use imagesc to display the matrix and set the colormap to red, yellow, and green by
colormap([0.9 0 0;1 1 0.08; 0.08 0.69 0.1])

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!