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

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?

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

답변 (1개)

Walter Roberson
Walter Roberson 2017년 9월 15일
For position A(J, K) the adjacent are
A( min( max(1, J-1:J+1), size(A,1)), K+1 )

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2017년 9월 15일

답변:

2017년 9월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by