Clear Filters
Clear Filters

How do I find the most stable N consecutive numbers?

1 view (last 30 days)
If I have a vector that has 2500 numbers. How can I find the most stable 50 consecutive numbers? By stable I mean 50 numbers that are consecutive and are close to each other (as in the mean difference between them is the smallest)

Accepted Answer

William Rose
William Rose on 17 Feb 2024
How about this:
x=rand(1,2500);
n=50;
stdx=zeros(1,length(x)-n+1); % allocate array for stdev(x(i:i+49))
for i=1:length(stdx)
stdx(i)=std(x(i:i+n-1));
end
[y,idx]=min(stdx);
fprintf('Minimum st.dev. segment with length %d starts at index %d, s.d.=%.3f.\n',...
n,idx,stdx(idx))
Minimum st.dev. segment with length 50 starts at index 1039, s.d.=0.234.
Good luck.
  2 Comments
William Rose
William Rose on 17 Feb 2024
The st.dev. for the the uniform distribution, with width 1, is 1/sqrt(12)=0.289. You can plot the st.dev. as a function of segment position within the vector:
x=rand(1,2500);
n=50;
stdx=zeros(1,length(x)-n+1); % allocate array for stdev(x(i:i+49))
for i=1:length(stdx)
stdx(i)=std(x(i:i+n-1));
end
[y,idx]=min(stdx);
fprintf('Minimum st.dev. segment with length %d starts at index %d, s.d.=%.3f.\n',...
n,idx,stdx(idx))
Minimum st.dev. segment with length 50 starts at index 2022, s.d.=0.229.
plot(1:length(stdx),stdx,'-b',idx,stdx(idx),'r*')
hold on; grid on;
yline(1/sqrt(12),'--g',Linewidth=2)
xlabel('Segment start position'); ylabel('St.Dev.')
title(['St.Dev.(segment with length ',num2str(n),')'])
legend('st.dev.','minimum','expected s.d.')
OK.
William Rose
William Rose on 17 Feb 2024
@John D'Errico makes a very good point about "mean difference". I took the liberty of assuming that you want a set of consecutive points whose values are similar, and I used standard deviation to quantify that idea. If you prefer to find the set with minimum mean absolute deviation, or minimum median absolute deviation, then use mad() instead of std().

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 17 Feb 2024
The mean difference? What is that exactly? In terms of mathematics?
Are you looking for the 50 element consecutive subset with the smallest standard deviation? Or perhaps the smallest maximum absolute deviation from the local mean? I could argue for either of those definitions, based on your question. I'm sure you may be thinking of something completely different, as I always seem to get these things wrong.
The smallest standard deviation is trivial. Download my movingstd utility from the file exchange. It will compute a sliding window standard deviation. Take the smallest, and you are done.
In the second case, I would compute a local sliding mean for a window of width 50. This is most simply done using conv. Now find the element in each sliding window that is maximally different from that sliding mean. This will not be difficult to do.
But again, I can't even guess what your real intent is here. So, what do the words "mean difference" describe in your mind?

Tags

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!