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!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
