How to find a minimum amount of equal adjacent values in an array?
3 views (last 30 days)
Show older comments
Lennard Pol
on 12 Nov 2020
Commented: Lennard Pol
on 18 Nov 2020
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 Comments
Bruno Luong
on 16 Nov 2020
Edited: Bruno Luong
on 16 Nov 2020
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.
Accepted Answer
Setsuna Yuuki.
on 16 Nov 2020
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
More Answers (0)
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!