How can I subtract elements in an array from the element before it.
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi guys,
I want to know how to subtract every element in an array from the element before it, for example element 2-element1 and element 3-element2 and so on.
採用された回答
help diff
DIFF Difference and approximate derivative.
DIFF(X), for a vector X, is [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)].
DIFF(X), for a matrix X, is the matrix of row differences,
[X(2:n,:) - X(1:n-1,:)].
DIFF(X), for an N-D array X, is the difference along the first
non-singleton dimension of X.
DIFF(X,N) is the N-th order difference along the first non-singleton
dimension (denote it by DIM). If N >= size(X,DIM), DIFF takes
successive differences along the next non-singleton dimension.
DIFF(X,N,DIM) is the Nth difference function along dimension DIM.
If N >= size(X,DIM), DIFF returns an empty array.
Examples:
h = .001; x = 0:h:pi;
diff(sin(x.^2))/h is an approximation to 2*cos(x.^2).*x
diff((1:10).^2) is 3:2:19
If X = [3 7 5
0 9 2]
then diff(X,1,1) is [-3 2 -3], diff(X,1,2) is [4 -2
9 -7],
diff(X,2,2) is the 2nd order difference along the dimension 2, and
diff(X,3,2) is the empty matrix.
See also GRADIENT, SUM, PROD.
Documentation for diff
doc diff
Other uses of diff
codistributed/diff iddata/diff tall/diff
datetime/diff sym/diff tireModel/diff
duration/diff symbolic/diff umat/diff
gpuArray/diff tabular/diff
11 件のコメント
so for a matrix I simply need to use diff(function name)
Essentially so. You say matrix however, but earlier you said vector. Diff can be applied to any desired dimension of a matrix.
And now you say function name. If a function returns an array of any size, then you can apply diff to that result.
So, yes.
Yes by function name I meant a function which returns an array. Thank you for all the help it is appreciated
John D'Errico
2024 年 3 月 27 日
編集済み: John D'Errico
2024 年 3 月 27 日
magic(5)
ans = 5x5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
Difference of pairs of columns:
diff(magic(5),[],2)
ans = 5x4
7 -23 7 7
-18 2 7 2
2 7 7 2
2 7 2 -18
7 7 -23 7
Difference between consecutive rows.
diff(magic(5),[],1)
ans = 4x5
6 -19 6 6 1
-19 1 6 6 6
6 6 6 1 -19
1 6 6 -19 6
That makes it more clear thank you. I have a function which returns an array of the size 639x1. So I need to do row 2-1 and row 3-2 etc until the final row.
Then you can just call diff, wih no other arguments. For example...
primes(20)
ans = 1x8
2 3 5 7 11 13 17 19
So the MATLAB function which returns a list of primes.
diff(primes(20))
ans = 1x7
1 2 2 4 2 4 2
That makes a lot of sense, but why is there is a 2 at the end of the result array if 11 is not being subtracted by anything?
Um, 19-17 == 2. That seems right to me. There are 8 numbers returned by primes in that call. There will be 7 differences.
Right my apologies I didn’t scroll right on my phone so I didn’t see the rest of the numbers, but that all makes perfect sense. Thank you for all the help and guidance.
Accepting the answer indicates that your problem has been solved (which can be helpful to other people in future) and it awards the volunteer with reputation points for helping you.
You can accept only 1 answer for a question, but you can vote for as many answers as you want. Voting an answer also provides reputation points.
Yes I will definitely accept it sorry about that
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Resizing and Reshaping Matrices についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
