making for loop picking out adjacent values of an 2D array
Show older comments
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 Comment
James Tursa
on 15 Sep 2017
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?
Answers (1)
Walter Roberson
on 15 Sep 2017
For position A(J, K) the adjacent are
A( min( max(1, J-1:J+1), size(A,1)), K+1 )
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!