Simple Array Calculation Help

조회 수: 1 (최근 30일)
David
David 2015년 6월 4일
편집: Nobel Mondal 2015년 6월 4일
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!

답변 (1개)

Nobel Mondal
Nobel Mondal 2015년 6월 4일
편집: Nobel Mondal 2015년 6월 4일
>> 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)';

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by