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!