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:
Fastest way to build a matric from Arrays

Subject: Fastest way to build a matric from Arrays

From: Hugh

Date: 7 Jul, 2011 14:37:07

Message: 1 of 5

Hi there,

I have an array (1*301) I want to repeat this row 301 times to create a 301*301 matrix. Also ideally I would like the original array to be (301*1)

what is the fastest way to do this horzcat or repmat or some other way?

http://www.mathworks.com/help/techdoc/math/f1-84864.html#f1-85019

Subject: Fastest way to build a matric from Arrays

From: dpb

Date: 7 Jul, 2011 15:03:45

Message: 2 of 5

On 7/7/2011 9:37 AM, Hugh wrote:
> Hi there,
>
> I have an array (1*301) I want to repeat this row 301 times to create a
> 301*301 matrix. Also ideally I would like the original array to be (301*1)
> what is the fastest way to do this horzcat or repmat or some other way?

Not sure w/o timing which would be fastest...

I'd sorta presume horzcat _might_ be somewhat slower than repmat but not
sure w/o testing...

a=repmat(v,1,length(v));

where v is the desired column vector and a the result does what you
wish. How it compares for something no bigger the 301x301 I'd guess
won't make much difference; that's really not all that big...

--

Subject: Fastest way to build a matric from Arrays

From: Greg von Winckel

Date: 7 Jul, 2011 15:09:25

Message: 3 of 5

> a=repmat(v,1,length(v));

Multiply the outer product with a vector of all ones is definitely faster than repmat in this situation.

a=v*ones(1,length(v))

Subject: Fastest way to build a matric from Arrays

From: Hugh

Date: 7 Jul, 2011 15:25:10

Message: 4 of 5

"Greg von Winckel" wrote in message <iv4i75$6l0$1@newscl01ah.mathworks.com>...
> > a=repmat(v,1,length(v));
>
> Multiply the outer product with a vector of all ones is definitely faster than repmat in this situation.
>
> a=v*ones(1,length(v))

thanks to both of you

Subject: Fastest way to build a matric from Arrays

From: dpb

Date: 7 Jul, 2011 21:02:04

Message: 5 of 5

On 7/7/2011 10:09 AM, Greg von Winckel wrote:
>> a=repmat(v,1,length(v));
>
> Multiply the outer product with a vector of all ones is definitely
> faster than repmat in this situation.
> a=v*ones(1,length(v))

Yeah...slaps forehead! :)

--

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