Stepwise substraction of all elements of a vector
1 view (last 30 days)
Show older comments
Hello everybody!
I have a Vector (720x1 double) and I would like to find the values where the delta is the biggest. So I want matlab to do the following:
(720,1)-(719,1)
(719,1)-(718,1)
.
.
.
How can I generate a vector, which contains this stepwise substraction?
So that all I need to do is, find the biggest value of this vector!
Thank you
Cheers
Christian
Accepted Answer
John BG
on 19 Jan 2017
Edited: John BG
on 19 Jan 2017
Hi Christian
The Star Strider answer wouldn't catch a narrow (negative) notch on a fairly flat signal because it would give as peak the flat plateau while the (negative) delta would get through unnoticed.
Since you mention 'values' in plural I suggest you use abs and findpeaks along with diff the following way
v=randi([-10 10],1,720);
w=abs(diff(v))
[peaks,loc_peaks]=findpeaks(abs(diff(v)),'MinPeakHeight',19)
stem(v);hold all;
plot(w)
plot(loc_peaks,peaks,'sg')
.
the field MinPeakHeight set to 19 catches just a few deltas. If you reduce this threshold too low too many peaks will show up and if you set it too high the function findpeaks doesn't get any.
I recommend you have a look at MATLAB help for function findpeaks because it has many optional fields to help choose what peaks to capture.
Christian
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG
More Answers (1)
Star Strider
on 19 Jan 2017
Edited: Star Strider
on 19 Jan 2017
If I understand correctly what you want to do, this will work:
v = randi(1000, 720, 1); % Create Data
[dvmax,dvmax_idx] = max(diff(v))
dvmax =
969
dvmax_idx =
333
The max function will find and return the value and index of the first occurrence of the maximum. If there are more and you want all of them, use the find function:
dvmax_idx = find(diff(v) == max(diff(v)))
You would have to find the value of the maximum with a separate call to max.
3 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!