Comparing values in two arrays: find those within a certain difference threshold
6 views (last 30 days)
Show older comments
Hello all,
I am processing a raw accelerometer signal, and have run into a filtering problem of sorts. My goal is to detect the valleys of interest in said signal: this includes only the detected valleys that occur soon after a detected peak. I have two arrays that I must compare: peakLoc (peak locations) and valleyLoc (valley locations), and want to save the values in valleyLoc that have a difference of 50 or less from a value in peakLoc. My current attempt is to compare the first value in peakLoc to every value in valleyLoc, and repeat with the next index of peakLoc until every value in peakLoc has been compared to the other array. If a value in valleyLoc meets the threshold, I want to save it into another array: (valleyLocrev). The values in both peakLoc and valleyLoc are in ascending order so technically not every value in valleyLoc need be compared to the value in peakLoc. Also, please note that peak Loc and valleyLoc may not be the same length.
So far, I have been attempting to use nested loops, but to no avail. Here is my code:
if length(valleyLoc)-length(peakLoc) <0 || length(valleyLoc)-length(peakLoc)==0
valleyLocrev=zeros(1,length(valleyLoc));
for countone= 1:length(valleyLoc)-1
counttwo= 1;
while counttwo<=length(peakLoc)
if abs(peakLoc(countone) - valleyLoc(counttwo)) < 20
valleyLocrev(countone)= valleyLoc(counttwo);
counttwo=counttwo+1;
else
valleyLocrev(countone)= NaN;
counttwo=counttwo+1;
end
end
end
else if length(valleyLoc)-length(peakLoc) >0
valleyLocrev=zeros(1,length(valleyLoc));
for countthree= 1:length(peakLoc)-1
countfour = 1;
while countfour<= length(valleyLoc)
if abs(peakLoc(countthree)-valleyLoc(countfour)) < 20
valleyLocrev(countthree)= valleyLoc(countfour);
countfour=countfour+1;
else
valleyLocrev(countthree) = NaN;
countfour=countfour+1;
end
end
end
end
end
Thanks for your help!
0 Comments
Answers (2)
Star Strider
on 19 Jul 2012
You didn't mention if you'd tried the ‘find’ function, so if you haven't, I suggest it to you. It could make your task much easier. It looks as though you're implementing something similar to it.
If ‘find’ won't solve your problem, please be a bit more specific about why the code you posted isn't working as you want it to.
Also, you mention in your question that you're looking for valleyLoc values to be less than 50 from the peakLoc values, but in your ‘if’ statements, you're testing for them to be <20.
1 Comment
Image Analyst
on 19 Jul 2012
Supplying some sample data would also help us figure out what's happening.
Jessica
on 20 Jul 2012
1 Comment
Star Strider
on 20 Jul 2012
Just to be clear, you've now solved your problem and no longer need our help?
See Also
Categories
Find more on Argument Definitions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!