Coordinates/values around number in array

1 次查看(过去 30 天)
Hello
Say I want to choose a value in an array and I want to find the value of the elements that surround them. Say the number i pick is 2 in the array below. I know I could find all the 2's and look north south east and west of each 2 in the array. I just wanted to know if there was a more elegent way
thank you
Andrew
3 1 1 1 1 %let say I pick "2", so the elements are 1,3,1,1,1,1,1,4
3 3 1 1 1
3 1 2 2 1
1 3 2 7 1
1 1 2 4 1

回答(1 个)

KSSV
KSSV 2022-5-8
If (i,j) is the index of number 2; then the nearest elements to it will be (i-1,j),(i+1,j),(i,j+1),(i,j-1),(i+1,j-1),(i+1,j+1),(i-1,j-1),(i-1,j+1).
Also have a look on knnsearch, rangesearch.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by