Thread Subject: Avoid double for loop

Subject: Avoid double for loop

From: Anish Goorah

Date: 31 Jan, 2008 15:49:02

Message: 1 of 4

Any ideas how I reduce this double for loop?

T = 40;
price = rand(T,1);
strike = rand(T,1); %%I know i can generate both of them
simultaneously but this is just a snippet to give you an
idea of what I want to do...

for i=1:T
    for j=1:T-1
        intrinsic(i,j) = price(j+1)-price(i);
    end
end

Subject: Avoid double for loop

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 31 Jan, 2008 16:14:55

Message: 2 of 4

In article <fnsqle$s8j$1@fred.mathworks.com>,
Anish Goorah <anish.goorah@gmail.com> wrote:
>Any ideas how I reduce this double for loop?

>T = 40;
>price = rand(T,1);
>strike = rand(T,1); %%I know i can generate both of them
>simultaneously but this is just a snippet to give you an
>idea of what I want to do...

>for i=1:T
> for j=1:T-1
> intrinsic(i,j) = price(j+1)-price(i);
> end
>end

p2T = price(2:T);
for i=1:T
  intrinsic(i,1:T-1) = p2T - price(i);
end
--
   So you found your solution
   What will be your last contribution?
   -- Supertramp (Fool's Overture)

Subject: Avoid double for loop

From: Arthur G

Date: 31 Jan, 2008 16:22:09

Message: 3 of 4

I'm curious what others think, but I just discovered the function
bsxfun as a great way to avoid for loops or temporary arrays. Your
double for loop can be replaced with the following code:

intrinsic =3D bsxfun(@minus, price(2:end)', price);

--Arthur G

On Jan 31, 10:49=A0am, "Anish Goorah" <anish.goo...@gmail.com> wrote:
> Any ideas how I reduce this double for loop?
>
> T =3D 40;
> price =3D rand(T,1);
> strike =3D rand(T,1); %%I know i can generate both of them
> simultaneously but this is just a snippet to give you an
> idea of what I want to do...
>
> for i=3D1:T
> =A0 =A0 for j=3D1:T-1
> =A0 =A0 =A0 =A0 intrinsic(i,j) =3D price(j+1)-price(i);
> =A0 =A0 end
> end

Subject: Avoid double for loop

From: Arthur G

Date: 31 Jan, 2008 17:15:23

Message: 4 of 4

On Jan 31, 11:22=A0am, Arthur G <gorramfr...@gmail.com> wrote:
> I'm curious what others think, but I just discovered the functionbsxfunas =
a great way to avoid for loops or temporary arrays. Your
> double for loop can be replaced with the following code:
>
> intrinsic =3Dbsxfun(@minus, price(2:end)', price);
>
> --Arthur G
>
> On Jan 31, 10:49=A0am, "Anish Goorah" <anish.goo...@gmail.com> wrote:
>
> > Any ideas how I reduce this double for loop?
>
> > T =3D 40;
> > price =3D rand(T,1);
> > strike =3D rand(T,1); %%I know i can generate both of them
> > simultaneously but this is just a snippet to give you an
> > idea of what I want to do...
>
> > for i=3D1:T
> > =A0 =A0 for j=3D1:T-1
> > =A0 =A0 =A0 =A0 intrinsic(i,j) =3D price(j+1)-price(i);
> > =A0 =A0 end
> > end

There's also a nice blog post on vectorizing calculations like yours:
http://blogs.mathworks.com/loren/2007/03/15/selecting-data-and-surrounding-v=
alues/

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
vectorize Anish Goorah 31 Jan, 2008 10:50:17
repmat Anish Goorah 31 Jan, 2008 10:50:17
rssFeed for this Thread

Contact us at files@mathworks.com