Thread Subject: Grouping of same numbers

Subject: Grouping of same numbers

From: Joe Frank

Date: 13 Mar, 2009 13:57:01

Message: 1 of 11

Hi

I have one vector:

A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1]

I'd like to group the same numbers and find out their indices ie. from where to where they are present:

Output should be like this
1{(ranges from)1,4}, 3{(ranges from)5,6}, 2{(ranges from)7,11}, 1{(ranges from)12,14}, etc

Is there any function for this? Can any1 please help me. I am new to Matlab and I dnt know much

Thanks,
Joe

Subject: Grouping of same numbers

From: tpl@eng.cam.ac.uk (Tim Love)

Date: 13 Mar, 2009 14:04:39

Message: 2 of 11

"Joe Frank" <sascod@gmail.com> writes:

>Hi

>I have one vector:

>A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1]

>I'd like to group the same numbers and find out their indices ie. from where to where they are present:

>Output should be like this
>1{(ranges from)1,4}, 3{(ranges from)5,6}, 2{(ranges from)7,11}, 1{(ranges from)12,14}, etc

>Is there any function for this?
No, but it would be a useful exercise for you to solve it using a "for" loop,
much as you would with most other languages.

Subject: Grouping of same numbers

From: Joe Frank

Date: 13 Mar, 2009 14:09:01

Message: 3 of 11

hey,

I wrote this.. But this is not working.. Im dont know what to do

A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1];
[row len]=size(A(1,:));
counter = len;
counter = counter -1;
for i=1:counter
if A(1,i)==A(1,i+1)
    disp(find(A(1,i)))
else
    disp(find(A(1,i+1)))
end
end

Please help me out

> No, but it would be a useful exercise for you to solve it using a "for" loop,
> much as you would with most other languages.

Subject: Grouping of same numbers

From: us

Date: 13 Mar, 2009 14:20:17

Message: 4 of 11

"Joe Frank"
> A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1]
> I'd like to group the same numbers and find out their indices ie. from where to where they are present:
> 1{(ranges from)1,4}, 3{(ranges from)5,6}, 2{(ranges from)7,11}, 1{(ranges from)12,14},...

one of the many solutions

     v=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1];
     r=arrayfun(@(x) find(v==x),unique(v),'uni',false);
     r{:}
%{
     ans = 1 2 3 4 12 13 14 21 22 % < r{1} := 1
     ans = 7 8 9 10 11 15 16 % <- ...
     ans = 5 6 19
     ans = 17 18 20
%}

us

Subject: Grouping of same numbers

From: Joe Frank

Date: 13 Mar, 2009 14:32:01

Message: 5 of 11

"us " <us@neurol.unizh.ch> wrote in message <gpdq31$940$1@fred.mathworks.com>...
> "Joe Frank"
> > A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1]
> > I'd like to group the same numbers and find out their indices ie. from where to where they are present:
> > 1{(ranges from)1,4}, 3{(ranges from)5,6}, 2{(ranges from)7,11}, 1{(ranges from)12,14},...
>
> one of the many solutions
>
> v=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1];
> r=arrayfun(@(x) find(v==x),unique(v),'uni',false);
> r{:}
> %{
> ans = 1 2 3 4 12 13 14 21 22 % < r{1} := 1
> ans = 7 8 9 10 11 15 16 % <- ...
> ans = 5 6 19
> ans = 17 18 20
> %}
>
> us


Thanks a lot.. But i need to keep them continous as in I showed in my Output..
Can u please guide me in doing that..

Subject: Grouping of same numbers

From: tpl@eng.cam.ac.uk (Tim Love)

Date: 13 Mar, 2009 14:54:29

Message: 6 of 11

"Joe Frank" <sascod@gmail.com> writes:

>Thanks a lot.. But i need to keep them continous as in I showed in my Output..
I started with your original code and tweaked it. Might need some more
tweaking, but it's a start.

A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1];
[row len]=size(A(1,:));
str=sprintf('%d{(ranges from)1',A(1,1));
for i=2:len
  if A(1,i)~=A(1,i-1)
    str=[str sprintf(',%d}, %d{(ranges from)%d',i-1,A(1,i),i)];
  end
end
disp([str sprintf(',%d}',i)]);

Subject: Grouping of same numbers

From: Joe Frank

Date: 13 Mar, 2009 15:03:04

Message: 7 of 11

tpl@eng.cam.ac.uk (Tim Love) wrote in message <gpds35$d2p$1@gemini.csx.cam.ac.uk>...
> "Joe Frank" <sascod@gmail.com> writes:
>
> >Thanks a lot.. But i need to keep them continous as in I showed in my Output..
> I started with your original code and tweaked it. Might need some more
> tweaking, but it's a start.
>
> A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1];
> [row len]=size(A(1,:));
> str=sprintf('%d{(ranges from)1',A(1,1));
> for i=2:len
> if A(1,i)~=A(1,i-1)
> str=[str sprintf(',%d}, %d{(ranges from)%d',i-1,A(1,i),i)];
> end
> end
> disp([str sprintf(',%d}',i)]);


Excellent.. Thanks a lot man..

Subject: Grouping of same numbers

From: Matt

Date: 13 Mar, 2009 15:17:01

Message: 8 of 11

"Joe Frank" <sascod@gmail.com> wrote in message <gpdond$7hv$1@fred.mathworks.com>...
> Hi
>
> I have one vector:
>
> A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1]
>
> I'd like to group the same numbers and find out their indices ie. from where to where they are present:
>
> Output should be like this
> 1{(ranges from)1,4}, 3{(ranges from)5,6}, 2{(ranges from)7,11}, 1{(ranges from)12,14}, etc
>
> Is there any function for this? Can any1 please help me. I am new to Matlab and I dnt know much

The unique() function will pretty much give this to you, if you use its extra output arguments.

Subject: Grouping of same numbers

From: Matt

Date: 13 Mar, 2009 15:19:01

Message: 9 of 11

"Joe Frank" <sascod@gmail.com> wrote in message <gpdond$7hv$1@fred.mathworks.com>...
> Hi
>
> I have one vector:
>
> A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1]
>
> I'd like to group the same numbers and find out their indices ie. from where to where they are present:
>
> Output should be like this
> 1{(ranges from)1,4}, 3{(ranges from)5,6}, 2{(ranges from)7,11}, 1{(ranges from)12,14}, etc
>
> Is there any function for this? Can any1 please help me. I am new to Matlab and I dnt know much
>
> Thanks,
> Joe

diff(sumsum(A)) will give you the points where the blocks begin...

Subject: Grouping of same numbers

From: us

Date: 13 Mar, 2009 15:20:17

Message: 10 of 11

"Joe Frank"
> Thanks a lot.. But i need to keep them continous as in I showed in my Output...

one of the solutions

% the data
     v=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1];
% the engine
     ix=[0,cumsum(diff(v)~=0)]+1;
     ix=[0,cumsum(accumarray(ix.',1).')];
     ix=[ix(1:end-1);ix(2:end)];
     r=arrayfun(@(x,y) x+find(v(x:y))-1,ix(1,:)+1,ix(2,:),'uni',false).';
% the result
     r{:}
%{
ans = 1 2 3 4 % <= 1
ans = 5 6
ans = 7 8 9 10 11
ans = 12 13 14 % <- 1
ans = 15 16
ans = 17 18
ans = 19
ans = 20
ans = 21 22
%}

us

Subject: Grouping of same numbers

From: Joe Frank

Date: 13 Mar, 2009 15:26:01

Message: 11 of 11

How Do I convert the code which u gave to me into a function?




tpl@eng.cam.ac.uk (Tim Love) wrote in message <gpdp5n$5av$1@gemini.csx.cam.ac.uk>...
> "Joe Frank" <sascod@gmail.com> writes:
>
> >Hi
>
> >I have one vector:
>
> >A=[1 1 1 1 3 3 2 2 2 2 2 1 1 1 2 2 4 4 3 4 1 1]
>
> >I'd like to group the same numbers and find out their indices ie. from where to where they are present:
>
> >Output should be like this
> >1{(ranges from)1,4}, 3{(ranges from)5,6}, 2{(ranges from)7,11}, 1{(ranges from)12,14}, etc
>
> >Is there any function for this?
> No, but it would be a useful exercise for you to solve it using a "for" loop,
> much as you would with most other languages.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
cumsum us 13 Mar, 2009 11:20:23
arrayfun us 13 Mar, 2009 10:25:06
code us 13 Mar, 2009 10:25:06
rssFeed for this Thread

Contact us at files@mathworks.com