Simple Array Calculation Help

1 vue (au cours des 30 derniers jours)
David
David le 4 Juin 2015
Modifié(e) : Nobel Mondal le 4 Juin 2015
Hello,
I have an array that contains S and R's (they are coded as numbers) which each represent a state. For simplicity lets say that S=sunny day R=rainy day. The array is about 1 million length.
Example: [S S S S R R R S S R S R]
I want to create a new array that records 1 or 0: record 0 if it was sunny for less than 3 days in a row, record 1 if it was sunny for 3 days or more in a row
so for the example above the array will look like this:
new_array=[1 0 0]
1 for the first entry because it was sunny 4 days in a row (>=3), 0 for the second entry because it was sunny for 2 days in a row (<3), 0 for the third entry because it was sunny for 1 day (<3),
If anyone could help. I would greatly appreciate it. Thank you!

Réponses (1)

Nobel Mondal
Nobel Mondal le 4 Juin 2015
Modifié(e) : Nobel Mondal le 4 Juin 2015
>> inputArray = [S R R R S S S R S R S S R R R R R S S S S S S R S S S R R S ];
>> result = (nonzeros((diff([0 (find(inputArray == R)) numel(inputArray)+1])-1))>2)';

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by