lengthData = 1500;
data = rand(lengthData,1);
result = zeros(floor(lengthData/2),lengthData);
for i = 1:floor(lengthData/2)-1
for j = 1+i:length(data)-i
if data(j) == max(data(j-i:j+i))
result(i,j)=1;
end
end
end
I have a data points of a specified lengthData
stored in variable data
. Now I am trying to find the maximum value in an specified interval, where interval lengths are increasing.
One quick observation is that as the interval size is increased, the number of entries in a particular row in result
is decreasing. Following is a plot of sum(result,2)
to verify that code is working as intended.
However, this code is taking a long time to execute. For larger values of lengthData
(of about 6000), time taken is almost 22 seconds ( up from 0.4 seconds with lengthData
of 1500).
Is there an alternate way I can implement my logic or to vectorize it, to somehow speed it up?
Based on this comment:
Maxima is occuring at the middle of window in my implementation of code. May be I was not clear in the problem description. I am looking for various maxima. There is only one global maxima but there are many local maxima. Now there local maximas can only exist when they are greater than numbers in their immediate vicinity. This vicinity I am considering as lenght of moving window. A particular value may be local maxima when sliding window is 5 but it may not be local maxima when sliding window is increased to 10.(...)
I suggest to take a look at findpeaks()
which does exactly that: finding local maxima. Your "sliding window length" would then be incorporated into the 'MinPeakDistance'
name-value pair, and the minimum height you mention in the question would be given by 'MinPeakHeight'
.