substract an element from previous element in a matrix

i have a k vector that contains 5 elements.
i want to write a code that substract - the second element from first
- the third element from second
- the fourth element from third
- the fifth element from fourth
and put them all in a vector form that will contain 4 elements
b=[k(2)-k(1) k(3)-k(2) k(4)-k(3) k(5)-k(4)];
how can i do that?

답변 (1개)

Star Strider
Star Strider 2022년 5월 10일

0 개 추천

The diff function seems to be appropriate.

댓글 수: 3

How about if i want to substract the square of each element minus the square of the previous one?
@Torsten — Thank you!

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

카테고리

질문:

2022년 5월 10일

댓글:

2022년 5월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by