Circular indexing of an array

98 vues (au cours des 30 derniers jours)
Wade
Wade le 31 Juil 2012
Modifié(e) : amir moin le 23 Juil 2021
I have run into this a number of times in a recent project I'm working on and was wondering what good solutions are out there. What happens is I often have an array representing positions on a line.
X = 1:10;
I want to create a function that indexes the code and circularly wraps around such that when I go through the first iteration of my loop idx-1 = 0 is interpreted as a 10 instead.
for i = 1:10
(X(i-1)+X(i))/2
end
I've tried appending the end element of the array on to the beginning.
X = [10 1:10]
for i = 2:11
...
Seems kind of like an ugly solution though. Any other suggestions?
Thanks!

Réponse acceptée

Walter Roberson
Walter Roberson le 31 Juil 2012
Modifié(e) : Walter Roberson le 31 Juil 2012
wrapN = @(x, N) (1 + mod(x-1, n));
N = length(X);
for i = 1:10
(X(wrapN(i-1,N))+X(wrapN(i,N)))/2
end
  2 commentaires
Abhinav Gupta
Abhinav Gupta le 30 Avr 2017
Just a minor typo in the wrapN function. It should be:
wrapN = @(x, n) (1 + mod(x-1, n));
Walter Roberson
Walter Roberson le 30 Avr 2017
Ah, right. Or to be consistent with my naming,
wrapN = @(x, N) (1 + mod(x-1, N));

Connectez-vous pour commenter.

Plus de réponses (6)

Oleg Komarov
Oleg Komarov le 31 Juil 2012
I have something different, a vectorized approach:
X = 1:10;
shift = 1;
B1 = (circshift(X,[1 shift]) + X)/2
B2 = (X([end-shift+1 1:end-shift]) + X)/2 %equivalent
isequal(B1,B2) % 1

Teja Muppirala
Teja Muppirala le 1 Août 2012
X = 1:10;
ifft(fft(X).*fft([1 1]/2,numel(X)))

bym
bym le 31 Juil 2012
Modifié(e) : bym le 31 Juil 2012
I don't know if it will help but you can use X as the index variable in the loop
X = [10 1:10]
for X
...
end
alternatively, if inside the loop is what you posted you can do
y = conv(X,[.5 .5]);
y([1 end]) = [];
without a loop

Andrei Bobrov
Andrei Bobrov le 1 Août 2012
Modifié(e) : Andrei Bobrov le 1 Août 2012
in this case
X = 1:10;
conv(X([end,1:end]),[1 1]/2,'valid');

Wade
Wade le 2 Août 2012
Thanks all!

amir moin
amir moin le 23 Juil 2021
Modifié(e) : amir moin le 23 Juil 2021
Taking you need a circle of length N=10; then you can set the array X(100) to the value of circular index Index(k)
N=10;
Index = @(i) mod(i,N)+(mod(i,N)==0).*N;
X = Index(1:100);
end
  3 commentaires
amir moin
amir moin le 23 Juil 2021
Sorry! I had made a mistake. Now the corrected solution is there. Thank you for your comment.
Walter Roberson
Walter Roberson le 23 Juil 2021
Seems complicated compared to my
wrapN = @(x, N) (1 + mod(x-1, N));
X = wrapN(1:100, 10);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by