Can I create a vector with a specific pattern in Matlab?

I have this following command:
n=100; ri=1:1:n
I would like to create a new vector according to this pattern: X=[(ri(3)-2*ri(2)+ri(1)),ri(4)-2*ri(3)+ri(2),...,ri(n)-2*ri(n-1)+ri(n-2)].
In words, I would like to create a vector in which its first element is composed by the the third element of ri minus 2*second element of ri plus the first element of ri. The second element would be ri(4)-2*ri(3)+2*ri(2). This pattern would be repeated until ri(n)-2*ri(n-1)+ri(n-2).
Can I create this vector with this specific pattern in Matlab? Sorry If I was not clear in my question.
Thank you very much!

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 13일
편집: Azzi Abdelmalek 2015년 7월 13일
n=100;
ri=1:1:n
v=[ ri(3:n)' ri(2:n-1)' ri(1:n-2)' ]
cr=[1 -2 1]
out=sum(bsxfun(@times,v,cr),2)
Or
n=100;
ri=1:1:n
m=3
v=bsxfun(@minus,ri(m:n)',0:m-1)
cr=[1 -2 1]
out=sum(bsxfun(@times,v,cr),2)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Aerospace Blockset에 대해 자세히 알아보기

질문:

2015년 7월 13일

댓글:

2015년 7월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by