Simple Array Calculation Help

4 views (last 30 days)
David
David on 4 Jun 2015
Edited: Nobel Mondal on 4 Jun 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!

Answers (1)

Nobel Mondal
Nobel Mondal on 4 Jun 2015
Edited: Nobel Mondal on 4 Jun 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)';

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!