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:
duplicate rows of a matrix

Subject: duplicate rows of a matrix

From: maxstirner

Date: 26 Mar, 2012 21:05:18

Message: 1 of 4

a have this matrix:
A=[1 2
     3 4
     5 6]
and i want to have:
A=[1 2
      1 2
      3 4
      3 4
      5 6
      5 6]
how can i achieve this?

Subject: duplicate rows of a matrix

From: Cory

Date: 26 Mar, 2012 21:25:17

Message: 2 of 4

"maxstirner " <dmtmakris@gmail.com> wrote in message <jkqlmd$7du$1@newscl01ah.mathworks.com>...
> a have this matrix:
> A=[1 2
> 3 4
> 5 6]
> and i want to have:
> A=[1 2
> 1 2
> 3 4
> 3 4
> 5 6
> 5 6]
> how can i achieve this?

You can generate a list like rowNum = 1 1 2 2 3 3 ... by doing rowNum = 1 + floor( (0:n)/2 ).

Then A = A(rowNum,:);

Subject: duplicate rows of a matrix

From: Matt J

Date: 26 Mar, 2012 21:28:13

Message: 3 of 4

"maxstirner " <dmtmakris@gmail.com> wrote in message <jkqlmd$7du$1@newscl01ah.mathworks.com>...
> a have this matrix:
> A=[1 2
> 3 4
> 5 6]
> and i want to have:
> A=[1 2
> 1 2
> 3 4
> 3 4
> 5 6
> 5 6]
> how can i achieve this?

One way,

A=kron(A,[1;1])

Another, and probably more efficient way

A=A(ceil(1:2*size(A,1)), :)

Subject: duplicate rows of a matrix

From: Roger Stafford

Date: 26 Mar, 2012 22:09:11

Message: 4 of 4

"Matt J" wrote in message <jkqn1d$bgm$1@newscl01ah.mathworks.com>...
> Another, and probably more efficient way
> A=A(ceil(1:2*size(A,1)), :)
- - - - - - - - -
  I think you mean

 A=A(ceil((1:2*size(A,1))/2), :);

Roger Stafford

Tags for this Thread

No tags are associated with 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