필터 지우기
필터 지우기

How to find a minimum amount of equal adjacent values in an array?

조회 수: 2 (최근 30일)
Lennard Pol
Lennard Pol 2020년 11월 12일
댓글: Lennard Pol 2020년 11월 18일
Hi all,
I have a column vector X of 3000 data points long (15 min averaged data). To check whether a certain system freezed for at least a certain period of time, I want to check this array for a minimum of 100 adjacent data points with the exact same value.
I started like this:
diff_X = diff(X);
movingsum_X = movsum(diff_X,100);
flatlines = find(movingsum_X == 0);
Despite this giving results that are partly satisfying, it doesn't do the trick all the way unfortunately.
Anyone that can help me out extending this to a fully working script? Other ways to reach the goal are welcome too, thanks.
Lennard
  댓글 수: 3
Bruno Luong
Bruno Luong 2020년 11월 16일
편집: Bruno Luong 2020년 11월 16일
Your code is flawed and fails if the signal is sum of pure oscillations (signal without DC component). Change
diff_X = diff(X)
to
diff_X = abs(diff(X))
will fix the flaw.
Lennard Pol
Lennard Pol 2020년 11월 18일
This improved the performance, thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Setsuna Yuuki.
Setsuna Yuuki. 2020년 11월 16일
You can test this code with different A matrix:
x = 2;
% X x 3000 matrix of example
% A = ones(x,1000); %there is always error
% A = [3*ones(2,25) 4*ones(2,48) 2*ones(2,2901)]; % there is error after 147 cycles
% A = rand(x,3000); % there is never error
B = reshape(A,1,x*length(A));
cont = 0; contM = 0;
for n = 1:length(B)-99 % n indicates the number that is repeated 100 times
for m = n:(n+99)
if(B(n) == B(m)) %Compare wit future values
cont = cont+1;
end
end
if (cont == 100) %If cont == 100 the system has stopped
fprintf("Error in system \n");
break; %Only if you want stop analysis
else
fprintf("Good luck \n"); %Good luck
end
cont = 0;
end

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by