필터 지우기
필터 지우기

how do i make a for loop picking out adjacent values of an 2D array

조회 수: 9 (최근 30일)
For example
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]
My current position is at A(3,3) which is equal to 6. How do I make a loop that will pick out the minimum adjacent values of 6 which are [7 5 1]

채택된 답변

Image Analyst
Image Analyst 2017년 9월 8일
Assuming you actually meant [7; 5; 1] instead of [7 5 1], you can get the three elements in the column to the right of some specified element like this:
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]
row = 3; % Whatever...
col = 3;
output = A(row-1:row+1, col+1)
  댓글 수: 1
Image Analyst
Image Analyst 2017년 9월 8일
You'd have to check the rows and columns to make sure they are not outside the array. Here is how to check the row:
row1 = max(1, row-1);
row2 = min(size(A, 1), row);
output = A(row1:row2, col+1)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Sparse Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by