How can I find the difference between adjacent elements while ignoring the first value?

I have the following column vector.
data = randperm(100,15)';
Is there a way of using the "diff" function to find the difference between adjacent elements while ignoring the first one? In other words, I would like the calculation to start with elements 2 and 3 then 4 and 5 etc.
Appreciate the help! Thanks.

 採用された回答

Stephen23
Stephen23 2021 年 2 月 15 日
diff(data(2:end))

5 件のコメント

KALYAN ACHARJYA
KALYAN ACHARJYA 2021 年 2 月 15 日
編集済み: KALYAN ACHARJYA 2021 年 2 月 15 日
:) 🙏
Benjamin Horsley
Benjamin Horsley 2021 年 2 月 15 日
As easy as that! Thank you. Safe to say I'm still very much learning!
Benjamin Horsley
Benjamin Horsley 2021 年 2 月 15 日
編集済み: Benjamin Horsley 2021 年 2 月 15 日
Sorry, just a quick follow-up question to this, @Stephen Cobeldick. If my vector has 14 elements, is there a way of returning only 7 difference values instead of 13? I mean to say I only want to find the difference between element 1 and 2 then skip ahead to elements 3 and 4 (skipping the difference between elements 2 and 3). Hope that makes sense. Again, really appreciate the feedback.
Stephen23
Stephen23 2021 年 2 月 15 日
編集済み: Stephen23 2021 年 2 月 15 日
Where vec is your vector of 14 elements:
out = vec(2:2:end)-vec(1:2:end)
Benjamin Horsley
Benjamin Horsley 2021 年 2 月 15 日
Terrific! Thanks, @Stephen Cobeldick!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by