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:
repeating array elements

Subject: repeating array elements

From: Adam Chapman

Date: 25 Jan, 2013 21:34:54

Message: 1 of 6

Can anybosy think of a fast way to do coppy each element of an array n times row or column wise?

For example, changing the array

1 2 3
4 5 6

to

1 1 2 2 3 3
1 1 2 2 3 3
4 4 5 5 6 6
4 4 5 5 6 6

I'm struggling to think of a way to vectorise this.

If anybody could help I'd be very greatful

Subject: repeating array elements

From: Bruno Luong

Date: 25 Jan, 2013 21:56:08

Message: 2 of 6

% First way:
[m n] = size(A);
B = reshape(A,1,m,1,n);
B = repmat(B, [2 1 2 1]);
B = reshape(B,[m n]*2)

% Second way
B = kron(A,ones(2))

% Bruno

Subject: repeating array elements

From: Bruno Luong

Date: 26 Jan, 2013 10:08:07

Message: 3 of 6

% Third method:
[m n] = size(A);
B = A(ceil((1:2*m)/2),ceil((1:2*n)/2))

% Bruno

Subject: repeating array elements

From: Adam Chapman

Date: 26 Jan, 2013 10:45:47

Message: 4 of 6

On Saturday, January 26, 2013 10:08:07 AM UTC, Bruno Luong wrote:
> % Third method:
>
> [m n] = size(A);
>
> B = A(ceil((1:2*m)/2),ceil((1:2*n)/2))
>
>
>
> % Bruno

Wow! Bruno is the biggest Matlab genius of them all :)

Subject: repeating array elements

From: Bruno Luong

Date: 26 Jan, 2013 15:40:08

Message: 5 of 6

% Another way:

[m n] = size(A);
B = A(repmat(1:m,2,1),repmat(1:n,2,1))

% Bruno

Subject: repeating array elements

From: Matt J

Date: 26 Jan, 2013 15:49:08

Message: 6 of 6

Adam Chapman <adamchapman1985@hotmail.co.uk> wrote in message <63207d27-e8c5-49a1-b1ed-f892f49929dd@googlegroups.com>...
> Can anybosy think of a fast way to do coppy each element of an array n times row or column wise?
>
> For example, changing the array
>
> 1 2 3
> 4 5 6
>
> to
>
> 1 1 2 2 3 3
> 1 1 2 2 3 3
> 4 4 5 5 6 6
> 4 4 5 5 6 6
==============

It's puzzling, of course, why you would want to simply copy data this way. It just takes up more memory.

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