making for loop picking out adjacent values of an 2D array

I have the matrix
A= [1 3 4 7 3;
5 4 3 7 2;
9 8 6 5 2;
9 6 3 1 6;
2 9 6 8 5]
how do i make a loop that when given the current position(which could be any element in the array) will give me the adjacent values
e.g. My current position is at A(3,3) which is equal to 6, i would want it to display [7;5;1].
and e.g My current position is at A(5,2), which is equal to 9. i would want it to display [3;6].
and e.g My current position is at A(1,4), which is equal to 7. i would want to display (3;2].
thanks

1 comentario

What have you done so far? What specific problems are you having with your code? What answer should you get if the element is on the right-most column?

Iniciar sesión para comentar.

Respuestas (1)

For position A(J, K) the adjacent are
A( min( max(1, J-1:J+1), size(A,1)), K+1 )

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 15 de Sept. de 2017

Respondida:

el 15 de Sept. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by