COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### finding consecutive numbers

• Follow

Hi all,

I am trying to write a program in matlab of counting consecutive numbers in a vector as 1, for example, for the following vector (ab) I want to count 41, 42 as 1 and 88, 89 as 1, this would make the vector size (ab) add up to 6 instead of 8:

ab= [41 42 50 55 73 88 89 100]

any help would be greatly appreciated.

Thankyou
 0

Ian wrote:
> Hi all,
>
> I am trying to write a program in matlab of counting consecutive numbers
> in a vector as 1, for example, for the following vector (ab) I want to
> count 41, 42 as 1 and 88, 89 as 1, this would make the vector size (ab)
>
> ab= [41 42 50 55 73 88 89 100]
>
> any help would be greatly appreciated.

>> ab= [41 42 50 55 73 88 89 100]
ab =
41    42    50    55    73    88    89   100
>> sum(diff(ab)==1)
ans =
2
>>

--
 0

dpb wrote:
....

>  >> ab= [41 42 50 55 73 88 89 100]
> ab =
>     41    42    50    55    73    88    89   100
>  >> sum(diff(ab)==1)
> ans =
>      2

>> % oh, btw...if can have descending as well as ascending
>> cd = -ab;
>> length(cd)-sum(diff(abs(cd))==1)
ans =
6
>>

--
 0

"Ian " <ikirby23@yahoo.com> wrote in message <i7aecd\$kt7\$1@fred.mathworks.com>...
> Hi all,
>
> I am trying to write a program in matlab of counting consecutive numbers in a vector as 1, for example, for the following vector (ab) I want to count 41, 42 as 1 and 88, 89 as 1, this would make the vector size (ab) add up to 6 instead of 8:
>
> ab= [41 42 50 55 73 88 89 100]
>
> any help would be greatly appreciated.
>
> Thankyou very much for your help dpb much appreciated. Sorry to be a pain but some of the vectors I am working with have 3 or 4 consecutive numbers, I also want to count them as 1, for example:

ab=[41 42 43 50 73 88 89 90 91 100]

so 41,42,43 would =1, and 88, 89, 90, 91 would also = 1 making the vector size add up to 5.
 0

Ian wrote:
> "Ian " <ikirby23@yahoo.com> wrote in message
> <i7aecd\$kt7\$1@fred.mathworks.com>...
>> Hi all,
>>
>> I am trying to write a program in matlab of counting consecutive
>> numbers in a vector as 1, for example, for the following vector (ab) I
>> want to count 41, 42 as 1 and 88, 89 as 1, this would make the vector
>>
>> ab= [41 42 50 55 73 88 89 100]
>>
>> any help would be greatly appreciated.
>>
>> Thankyou very much for your help dpb much appreciated. Sorry to be a
>> pain but some of the vectors I am working with have 3 or 4 consecutive
>> numbers, I also want to count them as 1, for example:
>
> ab=[41 42 43 50 73 88 89 90 91 100]
>
> so 41,42,43 would =1, and 88, 89, 90, 91 would also = 1 making the
> vector size add up to 5.

Then same idea but you've got another step--diff(diff(x))

The latter will give count of contiguous runs.

--
 0

Alternatively,

numel(regexp(sprintf('%i',diff(ab)==1),'1+'))
 0

5 Replies
879 Views

Similiar Articles:

7/24/2012 9:13:44 AM