Looping error where i want to get 5 instances of any same number in a column

2 views (last 30 days)
So i have a column of data which consists of numbers where i have to find 5 instances of the same number. So ,for example, 8,8,8,8,8,8,8,8,8,9,9,9,5,6,4,7,,6,2,3, etc. In this 8 has occurred 9 times. So i want to increment the count only once because even though there are nine 8's what my code is doing is taking the first 8 and getting the 5 consecutive numbers are incrementing. Then it takes the next 8 and increments the count and so on where the count becomes 5 but i want it to be 1. What i want is the first occurrence of any number to be the base value and take 5 consecutive numbers and increment the count. Then then take the 6th 8 and count if there are 5 consecutive 8's or that specific number or not. So, for example, 8,8,8,8,8,8,9,9,9,9,9,9,9,9,5,5,5,5,5,1,1,2,2,5,4,3,6,7,9,3,4,2,2,2,2,2,1,2,1. In this the count should be 4.
The code was :
count=0;
for i=1:length(data)-4
if data(i)==8 && sum(diff(data(i:i+4))==0)==4
count=count+1;
end
end
So in this code, it's only checking for 8 whereas i need it for all the numbers with 5 consecutive occurrences and it is checking for 5 consecutive but without skipping the first 5 taken into the consecutive count. I'm new to Matlab and this question is a bit tricky for me as i'm not good in logic building.
If(count_consecutive==5){
count_main=count_main+1; ...
a[i]=a[i+5];// continue for the base value. It should skip all the numbers that were counted in the consecutive count one and take the next number as the base for counting the consecutive numbers}
Thanks for any help. It would be greatly appreciated :).
  2 Comments
Karthik Garimella
Karthik Garimella on 26 Feb 2021
So the first one only has 5 consecutive 8's in the manner i specified. Hence the first one will have an output count =1 . In the second one, there are 4 of the 5 consecutive numbers of the same starting number. Hence the output would be 4, Another example i can give is 8,8,8,8,8,8,8,8,8,8(ten 8's),9,9,9,9,4,5,6,4,6,6,6,6,6. In this , the count should be 3 as it has 10 8's which increments the count to 2 and another 5 consecutive 6 which increments count one more time. Totla count would be 3.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 26 Feb 2021
With a little creativity, this can be done without using loops.
n = [8,8,8,8,8,8,9,9,9,9,9,9,9,9,5,5,5,5,5,1,1,2,2,5,4,3,6,7,9,3,4,2,2,2,2,2,1,2,1];
% identify when value changes
d = logical(diff(n));
% Use index position to determine number of consecutive numbers
ind = (1:length(n)-1);
L = diff([1 ind(d)]);
% Sount all occurrences with 5+ consecutive numbers (sum the result of a logical comparison)
count = sum(L>=5)
count = 4
  2 Comments
Cris LaPierre
Cris LaPierre on 26 Feb 2021
Based on your updated explanation, I'd modify my answer to the following
n = [8,8,8,8,8,8,8,8,8,8,9,9,9,9,4,5,6,4,6,6,6,6,6];
d = logical(diff(n));
ind = (1:length(n-1));
L = diff([0 ind(d) length(n)]);
sum(floor(L/5))
ans = 3

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!