I have a vector like on the figure bellow. I extracted the min and max values of it (A,B,C,D,E,F,G) and now I need to make a comparison of all the consecutive values and put the difference in another vector. In other words to find the difference between the values A with B; B with C etc. (named 1,2,3,4,5,6 on the figure).
And my code:
b=[1 2 3 4 5 6 7 8 9 8 7 6 5 4 5 6 7 8 9 10 7 4 3 2 1 4 7 8 9 7 6 5 4 3 4 5 6 7 8 3 2 1];
k=numel(b);
MIN=zeros(1,k);
MAX=zeros(1,k);
for i=3:k
if b(i-1)>b(i-2) && b(i-1)>b(i)
MAX(i-1)=b(i-1);
end
end
for i=3:k
if b(i-1)<b(i-2) && b(i-1)<b(i)
MIN(i-1)=b(i-1);
end
end
MIN=MIN(MIN~=0);
MAX=MAX(MAX~=0);

 채택된 답변

Thorsten
Thorsten 2016년 8월 16일
편집: Thorsten 2016년 8월 16일

0 개 추천

Index of extrema:
b = b(:);
idx = find(([b(1); b(1:end-1)] - b).*([b(2:end); b(end)] - b) > 0);
Difference between extrema
d = diff(b(idx));

댓글 수: 3

Miro Mitev
Miro Mitev 2016년 8월 16일
편집: Miro Mitev 2016년 8월 16일
It gives me this (for the index of extrema):
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Thorsten
Thorsten 2016년 8월 16일
I added b = b(:) to convert b to a column vector.
Miro Mitev
Miro Mitev 2016년 8월 16일
Thank you! It is exactly what i was searching for.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2016년 8월 16일

댓글:

2016년 8월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by