Asked by eden kisos
on 17 Nov 2019 at 17:21

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

...

Answer by Image Analyst
on 17 Nov 2019 at 17:33

Accepted Answer

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.

eden kisos
on 17 Nov 2019 at 19:51

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

Sign in to comment.

Answer by Steven Lord
on 17 Nov 2019 at 18:12

eden kisos
on 17 Nov 2019 at 19:56

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.