필터 지우기
필터 지우기

Periodic indexing of a matrix

조회 수: 6 (최근 30일)
andrea
andrea 2020년 3월 1일
댓글: Alexander 2024년 2월 27일
I need some help, i have a matrix rapresenting points on a grid and when given a element i would like to find the indices of its nearest neighbors keeping in mind that i have periodic boundary conditions, so that if i have the element A(1,1) its nearest neighbors are
  • A(1,N)
  • A(2,1)
  • A(1,2)
  • A(N, 1)
Where A is my matrix and N is the dimension, and i need a code which will find the indices of n.n of a given element. Thanks in advance
  댓글 수: 4
andrea
andrea 2020년 3월 1일
So what I'm doing is an ising model, my matrix is a matrix of 1 and -1 randomly chosen. I choose a random element of my matrix then I need to choose another element among the nearest neighbors randomly and i want its index.
Guillaume
Guillaume 2020년 3월 1일
I don't know what's an ising model.
"and i want its index."
What for?

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

채택된 답변

David Goodmanson
David Goodmanson 2020년 3월 1일
편집: David Goodmanson 2020년 3월 1일
Hi Andrea,
for N points, index j, in 1 dimension,
fnext largest index = mod(j,N)+1
next smallest index = mod(j-2,N)+1
then just include the non-changing index for 2 dimensions.
As mentioned by Guillaume, it might be convenient to have entire matrices of new indices. For example, all the indices for the next right neighbor is
indright = (mod(1:N)+1,1:N)
and so forth.
  댓글 수: 1
Alexander
Alexander 2024년 2월 27일
This worked like a charm! Though I believe it should be:
indright = (mod(1:N,N)+1,1:N)
so that circularly shifting a vector's components by one index should look something like this:
x = [2 4 5]
N=length(x)
x_shift = x(mod(1:N,N)+1)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by