# Re: Fast matrix filling in IDL

• Subject: Re: Fast matrix filling in IDL
• From: davidf(at)dfanning.com (David Fanning)
• Date: Fri, 11 Dec 1998 17:45:13 -0700
• Newsgroups: comp.lang.idl-pvwave
• Organization: Fanning Software Consulting
• References: <74s97q\$h6j\$1@nnrp1.dejanews.com>
• Xref: news.doit.wisc.edu comp.lang.idl-pvwave:13115

```Timm Weitkamp (weitkamp@my-dejanews.com) writes:

> What is the fastest way of filling a matrix with identical
> column vectors in IDL?
>
> More precisely, if A is a float matrix (m x n) and V is
> a vector with m elements, is there a faster way than
>
>   FOR i=0,n-1 DO A[*,i]=V ?

I don't know about "fastest", but

vector = Replicate(1, n)
array = V ## vector

is about 5 times faster for a 1000 by 500 array.
Here is my example program:

*************************************************************
PRO TEST

array = Fltarr(1000, 500)
v = RandomU(seed, 1000)

time = systime(1)
FOR i=0,500-1 DO array[*,i] = v
Print, 'Time for Loop: ', systime(1) - time

time = systime(1)
vector = Replicate(1, 500)
array = v ## vector
Print, 'Time for Matrix Operations: ', systime(1) - time
END
*************************************************************

And the results:

IDL> test
Time for Loop:                   0.10000002
Time for Matrix Operations:      0.019999981

Cheers,

David

--
David Fanning, Ph.D.
Fanning Software Consulting
Phone: 970-221-0438 E-Mail: davidf@dfanning.com
Coyote's Guide to IDL Progamming: http://www.dfanning.com/
Toll-Free IDL Book Orders: 1-888-461-0155

```