2 nearest neighbors of each side of vector
1 回表示 (過去 30 日間)
古いコメントを表示
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
...
0 件のコメント
採用された回答
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 件)
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.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!