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

조회 수: 14 (최근 30일)
Riah Wilkinson
Riah Wilkinson 2017년 3월 18일
댓글: Walter Roberson 2017년 3월 27일
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
Walter Roberson
Walter Roberson 2017년 3월 27일
.... Then don't use that code.
You have forgotten that chessboard distance does not stop at distance 1 or 2.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 3월 18일
편집: Walter Roberson 2017년 3월 18일
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)
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 3월 18일
Blue, green, what's the difference? ;-)
Walter Roberson
Walter Roberson 2017년 3월 18일
Another approach:
t = imdilate(X, ones(3,3)) + 1;
t(X==1) = 3;
cmap = [0 1 0;
1 1 0;
1 0 0];
image(t)
axis equal
colormap(cmap)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chad Greene
Chad Greene 2017년 3월 18일
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])
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 3월 18일
bwlabel is not enough in this case to figure out whether you are beside a 1.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by