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
|
|
|
|
Reply
|
Ian
|
9/21/2010 2:10:21 PM |
|
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)
> add up to 6 instead of 8:
>
> 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
|
|
|
|
Reply
|
dpb
|
9/21/2010 2:13:40 PM
|
|
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
|
|
|
|
Reply
|
dpb
|
9/21/2010 2:19:15 PM
|
|
"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
|
|
|
|
Reply
|
Ian
|
9/21/2010 2:49:20 PM
|
|
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
>> 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.
Then same idea but you've got another step--diff(diff(x))
The latter will give count of contiguous runs.
--
|
|
0
|
|
|
|
Reply
|
dpb
|
9/21/2010 3:01:28 PM
|
|
Alternatively,
numel(regexp(sprintf('%i',diff(ab)==1),'1+'))
|
|
0
|
|
|
|
Reply
|
Matt
|
9/21/2010 4:04:21 PM
|
|
|
5 Replies
879 Views
(page loaded in 0.09 seconds)
|