Count how many times a number appears for X number of rows (e.g 99 rows) in an array.

1 visualización (últimos 30 días)
I have a very long array made up of mostly 0s but also has rows with the number 5 in them. The 5s can occur for either 99 rows or 10 rows. In between the rows of either 99 5s or 10 5s, there are 0s. For example [0 0 0 0 5 5 5 5 5 5 5 5 5 5 0 0 0 0 5 5 ... x 99]
I want to write a script that can flag up in the array whenever the long list of 5s (99 rows) begins. The beginning of the list should always preceded by a 0. So far I have written the following code:
k = 1
for i = 1:length(data) %my array is called 'data'
if (data (i:i+98,3)== 5) & (data(i-1,3) == 0) %look for 5s that last 99 rows, and have a 0 before the first '5'
ninetyNINEdata {1}(k) = i;
k = k + 1; %count how many times these 99 rows of 5s occur
end
end
The issue is that I get the error 'Index exceeds matrix dimensions' when I run the code. Is there anyway to acheive the same outcome without this error occurring? Thank you!

Respuesta aceptada

Rik
Rik el 23 de Jul. de 2019
You have two options:
  1. use the runlength function by Jan (you can get it from the File Exchange)
  2. replace 1:length(data) by 1:(length(data)-98)
Also, you should replace length(data) by size(data,dim), where dim is the dimension you need.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by