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
James Tursa
2017년 9월 15일
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
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에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!