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!