How can i get neighbors from 3D array but in the same order as the matrix ?

조회 수: 4 (최근 30일)
math lcn
math lcn 2014년 11월 13일
댓글: Abhay 2014년 11월 14일
Hello,
How can i get neighbors from 3D array but in the same order as the matrix ?
for 2D array :
A =
0.6787 0.1712 0.0971 0.0344 0.1869
0.7577 0.7060 0.8235 0.4387 0.4898
0.7431 0.0318 0.6948 0.3816 0.4456
0.3922 0.2769 0.3171 0.7655 0.6463
0.6555 0.0462 0.9502 0.7952 0.7094
the neighbors of A(3,3) are :
ngh =
0.7060 0.8235 0.4387
0.0318 0.6948 0.3816
0.2769 0.3171 0.7655
the matlab code is :
A =rand(5,5) ;
idx = sub2ind(size(A), 3,3);
M = size(VV,1);
Nidx = [idx-M-1 idx-1 idx+M-1;...
idx-M idx idx+M; ...
idx-M+1 idx+1 idx+M+1];
ngh = A(Nidx(:));
ngh = reshape(ngh,3,3);
Can you help me to get the same results in the case of 3D array … ?
Regards
  댓글 수: 1
Abhay
Abhay 2014년 11월 14일
Dear Evan, Sorry for posting my query in the flagged content, actually when I was going through the flagged content I have found the query related to the generation of 3-d plot , I found a little connection of it with my previously asked problem, so in my eagerness I posted it there only, kindly forgive me for being a new user.

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

답변 (1개)

Evan
Evan 2014년 11월 13일
편집: Evan 2014년 11월 13일
One way to do it:
A = rand(5,5,5);
B = false(size(A));
B(3,3,3) = true; %Point around which you want to find neighbors
mask = imdilate(B,ones(3,3,3));
ngh = reshape(A(mask),3,3,3)
  댓글 수: 2
math lcn
math lcn 2014년 11월 13일
Even, Thank you for your answer.
but how can i get linear index of neighbors ?
Evan
Evan 2014년 11월 13일
ngh_idx = reshape(find(mask),3,3,3);

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

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by