Hi, I'm a beginner and I have tried to find the solution on internet with no results
Starting from a vector=[1;2;...;x] and a for loop n=1:x,
for each loop i want to read the elements in the vector in position "n-1", "n", "n+1" at same time.
The problem is for n=1 i need to read the values in position "x" "1" "2" and for n=x i need to read the values in position "x-1" "x" "1". Now i'm working in the for loop using 3 "if" to manage the operations the 3 cases (n=1, n=x, n=all the rest), or creating a new vector =[x;1;2 ...;x;1]
Is there a better solution? Thanks

 채택된 답변

Adam
Adam 2016년 12월 14일

0 개 추천

vector = [vector(end); vector; vector(1)];
for n = 2:x+1
...
end
would work.

댓글 수: 2

Manuel Bartoli
Manuel Bartoli 2016년 12월 14일
편집: Manuel Bartoli 2016년 12월 15일
The loop should be n=2:x-1
By the way I was thinking at something more...
for example
vector=[1;2;...;10]
for n=1:15
...n = 15 read index n and give me 5
...n = 1 read index n-3 and give me 8
I'm thinking for a general solution Thanks
Adam
Adam 2016년 12월 14일
The loop runs to x+1 because the vector is now 2 elements bigger than it was - i.e. a size of x+2.

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

추가 답변 (0개)

카테고리

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

질문:

2016년 12월 14일

편집:

2016년 12월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by