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

2 次查看(过去 30 天)
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.

请先登录,再进行评论。

采纳的回答

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