How do I find the most stable N consecutive numbers?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
William Rose
2024-2-17
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 个评论
William Rose
2024-2-17
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
2024-2-17
@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().
更多回答(1 个)
John D'Errico
2024-2-17
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!