Thread Subject: Need to prepare list of pairs of next n elements of array

Subject: Need to prepare list of pairs of next n elements of array

From: Sudheer Tumu

Date: 24 Nov, 2009 18:00:35

Message: 1 of 7

Hi All,

I am new to Matlab. I have a requierement

I have an array of more than 200000 rows.

eg: A=[1 2
          2 3
          3 4
          4 5
          5 6
          6 7
          7 8
          8 9
          ...]
What I want to I want to prepare an array every n elements pairs.

say n=3, I need to get an array like
B=[ 1 2 2 3
      1 2 3 4
      1 2 4 5
      2 3 3 4
      2 3 4 5
      2 3 5 6
      3 4 4 5
      3 4 5 6
      3 4 6 7
     ..........] like this. For every row, I have to create pair of next three rows. for first row [1 2], the next 3 elements are 2 3, 3 4 , 4 5 like this.
the value n (here three) should be like a parameter. it may change any time.

I should not use loops.Please tell me any matlab command to do this.

Thank you,
TUMU

Subject: Need to prepare list of pairs of next n elements of array

From: Bruno Luong

Date: 24 Nov, 2009 19:46:35

Message: 2 of 7

One way:

[I J]=ndgrid(1:3,1:size(A,1)-3);
B = [A(J,:) A(I+J,:)]

Bruno

Subject: Need to prepare list of pairs of next n elements of array

From: Sudheer Tumu

Date: 25 Nov, 2009 03:06:20

Message: 3 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <hehd6r$bgd$1@fred.mathworks.com>...
> One way:
>
> [I J]=ndgrid(1:3,1:size(A,1)-3);
> B = [A(J,:) A(I+J,:)]
>
> Bruno

Hi Bruno,

Thank you for your valuable reply. But the problem is, the elements of the array are not in sorted order. You can not get the elements by I+J , because they will be in random order.
Do you have a work around for this problem.

Thank you,
Sudheer Tumu.

Subject: Need to prepare list of pairs of next n elements of array

From: ImageAnalyst

Date: 25 Nov, 2009 03:15:46

Message: 4 of 7

Huh? What do you mean "sorted"?
Bruno's clever solution gives you exactly the B that you requested!

Subject: Need to prepare list of pairs of next n elements of array

From: Sudheer Tumu

Date: 25 Nov, 2009 03:37:04

Message: 5 of 7

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0150bf21-f140-49e0-9a75-8b04481f9c02@f16g2000yqm.googlegroups.com>...
> Huh? What do you mean "sorted"?
> Bruno's clever solution gives you exactly the B that you requested!

Hi,

In my main post , I gave

A=[1 2
          2 3
          3 4
          4 5
          5 6
          6 7
          7 8
          8 9
          ...]
But If A=[1 2
              5 9
              2 7
              1 6
              4 9
             ....]

I will not get
B=[ 1 2 5 9
      1 2 2 7
      1 2 1 6
      5 9 2 7
      5 9 1 6
      5 9 4 9
     ......]
like this with Bruno's solution.

Thank you,
Sudheer Tumu.

Subject: Need to prepare list of pairs of next n elements of array

From: Bruno Luong

Date: 25 Nov, 2009 07:23:03

Message: 6 of 7

"Sudheer Tumu" <tumusudheer@gmail.com> wrote in message <hei8p0$fqp$1@fred.mathworks.com>...

> But If A=[1 2
> 5 9
> 2 7
> 1 6
> 4 9
> ....]
>
> I will not get
> B=[ 1 2 5 9
> 1 2 2 7
> 1 2 1 6
> 5 9 2 7
> 5 9 1 6
> 5 9 4 9
> ......]
> like this with Bruno's solution.
>

Here is what I get with the code I gave above:
 
>> A=[1 2
              5 9
              2 7
              1 6
              4 9 ]
>>
[I J]=ndgrid(1:3,1:size(A,1)-3);
B = [A(J,:) A(I+J,:)]

B =

     1 2 5 9
     1 2 2 7
     1 2 1 6
     5 9 2 7
     5 9 1 6
     5 9 4 9

I can't see the difference if what you ask???

Bruno

Subject: Need to prepare list of pairs of next n elements of array

From: Sudheer Tumu

Date: 26 Nov, 2009 04:11:03

Message: 7 of 7

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <heim0n$dlr$1@fred.mathworks.com>...
> "Sudheer Tumu" <tumusudheer@gmail.com> wrote in message <hei8p0$fqp$1@fred.mathworks.com>...
>
> > But If A=[1 2
> > 5 9
> > 2 7
> > 1 6
> > 4 9
> > ....]
> >
> > I will not get
> > B=[ 1 2 5 9
> > 1 2 2 7
> > 1 2 1 6
> > 5 9 2 7
> > 5 9 1 6
> > 5 9 4 9
> > ......]
> > like this with Bruno's solution.
> >
>
> Here is what I get with the code I gave above:
>
> >> A=[1 2
> 5 9
> 2 7
> 1 6
> 4 9 ]
> >>
> [I J]=ndgrid(1:3,1:size(A,1)-3);
> B = [A(J,:) A(I+J,:)]
>
> B =
>
> 1 2 5 9
> 1 2 2 7
> 1 2 1 6
> 5 9 2 7
> 5 9 1 6
> 5 9 4 9
>
> I can't see the difference if what you ask???
>
> Bruno

Hi Bruno , Sorry. It was my mistake. It is working well. Thanks a lot.

Thank you,
Sudheer Tumu.

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com