Periodic indexing of a matrix
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
andrea
am 1 Mär. 2020
Kommentiert: Alexander
am 27 Feb. 2024
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 Kommentare
Akzeptierte Antwort
David Goodmanson
am 1 Mär. 2020
Bearbeitet: David Goodmanson
am 1 Mär. 2020
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 Kommentar
Alexander
am 27 Feb. 2024
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)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!