How to make a +5 (or any +number) jump in array values inside a for loop?

12 次查看(过去 30 天)
So when line 0 condition is met, I want the for loop to jump 5 rows from the given data set of a single column. So when count1 ==5 , I want the loop to skip 5 rows from the column and take the 6th element as the new reference. [8,8,8,8,8,9,6,546,6,5,6,4,5,5,5,5,5]. In this dataset, the count value should be 2. Now what the issue is that the code given below does not skip to the 6th reference value to compare the next 5 values. I tried line 1 and 2 but those did not work or have any effect on the code.
count=0;
count1=0;
for i=1:length(data)-4
for j=i+1:i+4
if data(i)~=data(j)
count1=0;
break;
else
count1=count1+1;
end
if count1==5 % line 0
count=count+1;
%data(i,1)=data(i+5,1); //line 1
%data(i)=data(i+5); //line 2
else
continue;
end
end
end
  5 个评论
Karthik Garimella
编辑:Karthik Garimella 2021-3-1
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. Total count would be 3. My problem is that i'm not able to skip/jump the array index from x to x+5 in the for loop when my condition gets satisfied. for ones(1,10) i'm assuming that the array would be of 10 ones [1,1,1,1,1,1,1,1,1,1]? If this is the case then the count would be 2.
Jan
Jan 2021-3-1
编辑:Jan 2021-3-1
I do not know, what "the first one" and "the second one" are.
What does "there are 4 of the 5 consecutive numbers of the same starting number" mean?
As far as I understand, you want to:
count the number of blocks, which have 5 equal elements.
This is a much shorter description, isn't it?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2021-3-1
编辑:Jan 2021-3-1
You can use FEX: RunLength , or this part taken from it:
function n = RunLength_count(x)
d = [true, diff(x(:).') ~= 0, true]; % TRUE if values change
n = diff(find(d)); % Number of repetitions
end
Then:
x = [8,8,8,8,8,9,6,546,6,5,6,4,5,5,5,5,5];
n = RunLength_count(x);
Result = sum(floor(n / 5))
USing floor(n / 5) let all blocks with less then 5 element be counted as zeros. 9 consecutive elements are counted as 1 block and 10 as 2 blocks.
You can do this with a loop also, but this is less elegant and efficient:
count = 0;
block = 0;
q = nan; % Initial element to compare with
for k = 1:numel(data)
if data(k) == q % Element equals former one
block = block + 1;
if block == 5
count = count + 1;
block = 0;
end
else % A new value
q = data(k);
block = 1;
end
end
You do not need a 2nd loop.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by