Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
consecutive numbers

Subject: consecutive numbers

From: fmri

Date: 5 Jul, 2013 13:16:12

Message: 1 of 9

Hi all,

I posted this before but I need something else with it :-)

I have a vector that has consecutive number every x or b (so they are repeated randomly ) and then another consecutive numbers that are not related to the previous one stored in another different length.

example :
x= [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ]
What I want is to represent every ( length of consecutive number by a single number So I want:
y= [ 1 1 1 2 2 2 2 3 3 4 4 4 4 4]

I was able to do this in excel by this command (assuming our consecutive column in C and started at C2 and the new column is J )
=IF(C3=C2+1,J2,J2+1)

I tried to do this in matlab but it did not work ( I tried to do something similar to the excel code )
for i=1:4741 , if ss((i+1),1)==ss((i),1)+1, b,b((i),1)+1 end end

So i = 1:4741 is the length of the consecutive numbers

Any suggestions please

Thanks

AS

Subject: consecutive numbers

From: fmri

Date: 6 Jul, 2013 18:05:10

Message: 2 of 9

"fmri" wrote in message <kr6gus$b8d$1@newscl01ah.mathworks.com>...
> Hi all,
>
> I posted this before but I need something else with it :-)
>
> I have a vector that has consecutive number every x or b (so they are repeated randomly ) and then another consecutive numbers that are not related to the previous one stored in another different length.
>
> example :
> x= [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ]
> What I want is to represent every ( length of consecutive number by a single number So I want:
> y= [ 1 1 1 2 2 2 2 3 3 4 4 4 4 4]
>
> I was able to do this in excel by this command (assuming our consecutive column in C and started at C2 and the new column is J )
> =IF(C3=C2+1,J2,J2+1)
>
> I tried to do this in matlab but it did not work ( I tried to do something similar to the excel code )
> for i=1:4741 , if ss((i+1),1)==ss((i),1)+1, b,b((i),1)+1 end end
>
> So i = 1:4741 is the length of the consecutive numbers
>
> Any suggestions please
>
> Thanks
>
> AS


Updates

Subject: consecutive numbers

From: dpb

Date: 6 Jul, 2013 20:03:20

Message: 3 of 9

On 7/5/2013 8:16 AM, fmri wrote:
...

> example :
> x= [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ]
> What I want is to represent every ( length of consecutive number by a
> single number So I want:
> y= [ 1 1 1 2 2 2 2 3 3 4 4 4 4 4]
...

 >> x= [1 2 3 7 8 9 10 13 14 20 21 22 23 24 ];
 >> s=splitvec(d,'value'); % Bruno's SplitVec @ FEX
 >> cnts=cellfun(@length,s(cellfun(@(x) isequal(x,1),s) | ...
                           cellfun(@length,s)>1))+1;
 >> res=[];for i=1:length(cnts),res=horzcat(res,repmat(i,1,cnts(i)));end
 >> res
res =
  1 1 1 2 2 2 2 3 3 4 4 4 4 4
 >>

One should be able to write the loop w/ nested anon functions but ran
out of time...

I like the cnts logic! :) There's probably a simpler way but this one
came to me as a way to find it and then it was a challenge to implement
it in that manner rather than looking for different...

--

Subject: consecutive numbers

From: dpb

Date: 6 Jul, 2013 21:02:54

Message: 4 of 9

On 7/6/2013 3:03 PM, dpb wrote:
> On 7/5/2013 8:16 AM, fmri wrote:
> ...
>
>> example :
>> x= [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ]
...
>> ... I want:
>> y= [ 1 1 1 2 2 2 2 3 3 4 4 4 4 4]
> ...
>
> >> x= [1 2 3 7 8 9 10 13 14 20 21 22 23 24 ];
> >> s=splitvec(d,'value'); % Bruno's SplitVec @ FEX
...

> I like the cnts logic! :)...

It dawned on me just as I walked away that the above logic isn't
general; it works for the specific case because there's no consecutive
values that aren't separated by other than one--a sequence of any fixed
delta would be counted just looking at length of split cells.

OTOH the following should work for any vector for consecutive values...

 >> cnts=cellfun(@length,s(cellfun(@(x) all(x==1),s)))+1;
 >> res=arrayfun(@(x,y) (repmat(y,1,x)), cnts,(1:length(cnts)) ...
                                ,'uniformoutput',false);
 >> res=[res{:}]
res =
   1 1 1 2 2 2 2 3 3 4 4 4 4 4
 >>

The above finds all cells in the split vector in which all values are
1's; those are the consecutive values. The length of the difference
vector is always one less than the number, hence the +1. One more
practice at writing anon function and arrayfun (I have only recently had
a release of Matlab that includes these so I'm still pretty much a
novice w/ them....couldn't get nested repmat and horzcat to play well to
gether as arrayfun intervened first always complaining about nonuniform
output before horzcat got a chance't at it...not sure about that one at
the moment as far as a way to avoid the intermediary. The loop may be
as good if just preallocate--you can determine the necessary lengths
easily enough.

--

Subject: consecutive numbers

From: james bejon

Date: 6 Jul, 2013 21:37:12

Message: 5 of 9

% If I've understood the request correctly:

x = [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ].';
d = cumsum([ 1 ; diff(x) ~= 1])

Subject: consecutive numbers

From: dpb

Date: 7 Jul, 2013 00:12:58

Message: 6 of 9

On 7/6/2013 4:37 PM, james bejon wrote:
> % If I've understood the request correctly:
>
> x = [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ].';
> d = cumsum([ 1 ; diff(x) ~= 1])

Nicely done. I didn't think about cumsum() accounting for the "short by
one" numbers in the diff() vector.

--

Subject: consecutive numbers

From: fmri

Date: 7 Jul, 2013 01:29:10

Message: 7 of 9

dpb <none@non.net> wrote in message <kra0lq$vhu$1@speranza.aioe.org>...
> On 7/6/2013 3:03 PM, dpb wrote:
> > On 7/5/2013 8:16 AM, fmri wrote:
> > ...
> >
> >> example :
> >> x= [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ]
> ...
> >> ... I want:
> >> y= [ 1 1 1 2 2 2 2 3 3 4 4 4 4 4]
> > ...
> >
> > >> x= [1 2 3 7 8 9 10 13 14 20 21 22 23 24 ];
> > >> s=splitvec(d,'value'); % Bruno's SplitVec @ FEX
> ...
>
> > I like the cnts logic! :)...
>
> It dawned on me just as I walked away that the above logic isn't
> general; it works for the specific case because there's no consecutive
> values that aren't separated by other than one--a sequence of any fixed
> delta would be counted just looking at length of split cells.
>
> OTOH the following should work for any vector for consecutive values...
>
> >> cnts=cellfun(@length,s(cellfun(@(x) all(x==1),s)))+1;
> >> res=arrayfun(@(x,y) (repmat(y,1,x)), cnts,(1:length(cnts)) ...
> ,'uniformoutput',false);
> >> res=[res{:}]
> res =
> 1 1 1 2 2 2 2 3 3 4 4 4 4 4
> >>
>
> The above finds all cells in the split vector in which all values are
> 1's; those are the consecutive values. The length of the difference
> vector is always one less than the number, hence the +1. One more
> practice at writing anon function and arrayfun (I have only recently had
> a release of Matlab that includes these so I'm still pretty much a
> novice w/ them....couldn't get nested repmat and horzcat to play well to
> gether as arrayfun intervened first always complaining about nonuniform
> output before horzcat got a chance't at it...not sure about that one at
> the moment as far as a way to avoid the intermediary. The loop may be
> as good if just preallocate--you can determine the necessary lengths
> easily enough.
>
> --

Hi dpb,

thanks for this. Honestly, I was facing some problems while applying yr commands. I am still learning the basic functions and always get confused when I see cellfun and arrayfun ...

Subject: consecutive numbers

From: fmri

Date: 7 Jul, 2013 01:30:13

Message: 8 of 9

"james bejon" wrote in message <kra2m8$87v$1@newscl01ah.mathworks.com>...
> % If I've understood the request correctly:
>
> x = [ 1 2 3 7 8 9 10 13 14 20 21 22 23 24 ].';
> d = cumsum([ 1 ; diff(x) ~= 1])


James, Thanks a lot. it works and in all the vectors that I have. Tested and all of them matched the results from excel.

THANK U

Subject: consecutive numbers

From: dpb

Date: 7 Jul, 2013 03:01:46

Message: 9 of 9

On 7/6/2013 8:29 PM, fmri wrote:
...

>> > >> x= [1 2 3 7 8 9 10 13 14 20 21 22 23 24 ];
>> > >> s=splitvec(d,'value'); % Bruno's SplitVec @ FEX
>> ...
...
>> OTOH the following should work for any vector for consecutive values...
>>
>> >> cnts=cellfun(@length,s(cellfun(@(x) all(x==1),s)))+1;
>> >> res=arrayfun(@(x,y) (repmat(y,1,x)), cnts,(1:length(cnts)) ...
>> ,'uniformoutput',false);
>> >> res=[res{:}]
...

> Hi dpb,
> thanks for this. Honestly, I was facing some problems while applying yr
> commands. I am still learning the basic functions and always get
> confused when I see cellfun and arrayfun ...

I'm still learning them meself having only had a release that contains
them <1 yr and not actively doing any technical work (my day job is
farming now). So, when I thought about the length vectors as a way I
took it as a challenge to use it... :)

BTW, it would be a little cleaner to get the final result above as

res=cell2mat(arrayfun(@(x,y) (repmat(y,1,x)), cnts,(1:length(cnts)), ...
                              'uniformoutput',false));

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us