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!

 채택된 답변

Walter Roberson
Walter Roberson 2012년 7월 31일
편집: Walter Roberson 2012년 7월 31일

8 개 추천

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

Just a minor typo in the wrapN function. It should be:
wrapN = @(x, n) (1 + mod(x-1, n));
Ah, right. Or to be consistent with my naming,
wrapN = @(x, N) (1 + mod(x-1, N));

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

추가 답변 (6개)

Oleg Komarov
Oleg Komarov 2012년 7월 31일

2 개 추천

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 2012년 8월 1일

1 개 추천

X = 1:10;
ifft(fft(X).*fft([1 1]/2,numel(X)))
bym
bym 2012년 7월 31일
편집: bym 2012년 7월 31일

0 개 추천

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 2012년 8월 1일
편집: Andrei Bobrov 2012년 8월 1일

0 개 추천

in this case
X = 1:10;
conv(X([end,1:end]),[1 1]/2,'valid');
Wade
Wade 2012년 8월 2일

0 개 추천

Thanks all!
amir moin
amir moin 2021년 7월 23일
편집: amir moin 2021년 7월 23일

0 개 추천

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

Walter Roberson
Walter Roberson 2021년 7월 23일
With that code, Index(1) would be 1, Index(9) would be 9, Index(10) would be 1, index(11) would be 1 as well. Only 9 out of the 10 locations would be used, and location 1 would be used twice.
https://www.mathworks.com/matlabcentral/answers/44993-circular-indexing-of-an-array#comment_450173 has the correct code.
amir moin
amir moin 2021년 7월 23일
Sorry! I had made a mistake. Now the corrected solution is there. Thank you for your comment.
Seems complicated compared to my
wrapN = @(x, N) (1 + mod(x-1, N));
X = wrapN(1:100, 10);

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2012년 7월 31일

편집:

2021년 7월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by