How do I find the most stable N consecutive numbers?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ali Almakhmari
le 17 Fév 2024
Commenté : William Rose
le 17 Fév 2024
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)
0 commentaires
Réponse acceptée
William Rose
le 17 Fév 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))
Good luck.
2 commentaires
William Rose
le 17 Fév 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))
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
le 17 Fév 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().
Plus de réponses (1)
John D'Errico
le 17 Fév 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?
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!