필터 지우기
필터 지우기

Taking difference of adjecent elements by seting a custom order

조회 수: 1 (최근 30일)
Ali Akbar
Ali Akbar 2014년 11월 12일
답변: Geoff Hayes 2014년 11월 13일
Hi
I have the following vector:
[1 2 3 4 5 6]
I want to take the the difference between the third element and the first (3-1) and then fourth and second element (4-2) and so on. How do I do this?
Thank you!

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 11월 13일
Ali - you could create two vectors of indices that you can then use to perform the subtraction. One vector would be all indices starting from 3, 4, 5,... up to the last index (which would correspond to the length of the input vector), and the other vector would be all indices starting from 1, 2, 3,... up to two less than the end of the input vector. Try the following
A = [1 2 3 4 5 6];
idx2 = 3:length(A);
idx1 = 1:length(A)-2;
myDiff = A(idx2) - A(idx1);
Running the above returns
myDiff =
2 2 2 2
which correspond to the differences of 3-1, 4-2, 5-3, and 6-4.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by