2 nearest neighbors of each side of vector

조회 수: 3 (최근 30일)
eden kisos
eden kisos 2019년 11월 17일
댓글: eden kisos 2019년 11월 17일
I have two vectors:
A= 2 8 3 6 1 9 5 4 3 7 10 %ranperm(10)
i=1:10
I would like to find for every i , the two closest neighbors of each side.
For example:
for i=1
neighbors= 2,8
for i=2
neighbors= 2,3,6
for i=3
neighbors= 2,8,6,1
for i=10
neighbors= 4,3
...

채택된 답변

Image Analyst
Image Analyst 2019년 11월 17일
This will do it:
A= [2 8 3 6 1 9 5 4 3 7 10] %ranperm(10)
for k = 1 : length(A)
firstIndex = max(k-1, 1);
lastIndex = min(k+1, length(A));
neighbors{k} = A(firstIndex:lastIndex);
end
celldisp(neighbors) % Display in command window.
  댓글 수: 1
eden kisos
eden kisos 2019년 11월 17일
I'm sorry, but I made a mistake. I'll write it down again.
A= 2 8 3 6 1 9 5 4 7 10 %ranperm(10)
i=1:10
I would like to find for every i , the two closest neighbors of each side.
For example:
for i=1
neighbors= 8,3
for i=2
neighbors= 2,3,6
for i=3
neighbors= 2,8,6,1
for i=10
neighbors= 4,7

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

추가 답변 (1개)

Steven Lord
Steven Lord 2019년 11월 17일
Are there specific operations you want to perform on those neighbors? The moving statistics functions on this documentation page may perform those operations directly (if you want to add them see movsum, for example) or serve as building blocks to help you perform those operations.
  댓글 수: 1
eden kisos
eden kisos 2019년 11월 17일
thank you, I am making some modifications to what is being received but the rest I already know how to do. I got stuck with the neighbors (especially the edges)

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

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by