How do I find the most stable N consecutive numbers?

조회 수: 2 (최근 30일)
Ali Almakhmari
Ali Almakhmari 2024년 2월 17일
댓글: William Rose 2024년 2월 17일
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)

채택된 답변

William Rose
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))
Minimum st.dev. segment with length 50 starts at index 1039, s.d.=0.234.
Good luck.
  댓글 수: 2
William Rose
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))
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 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
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?

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by