Thread Subject: how to vectorize this in matlab

Subject: how to vectorize this in matlab

From: matlab_learner

Date: 19 Mar, 2010 00:13:30

Message: 1 of 3

hi all
i want to eliminate the for statement...but the boundaries need to be
fixed. is there a better way of writing this? i am using matlab.
thanks

    d(1) = fi(2) - a*fi(1);
    d(79) = fi(80) - c*fi(81);
    for i = 3:79
        d(i) = fi(i-1)
    end


where a and c are scalars.

Subject: how to vectorize this in matlab

From: William Smith

Date: 19 Mar, 2010 00:31:25

Message: 2 of 3

matlab_learner <cibeji@gmail.com> wrote in message <91bf19f3-7065-4fe4-a6e5-1b7395c3eb8c@w27g2000pre.googlegroups.com>...
> hi all
> i want to eliminate the for statement...but the boundaries need to be
> fixed. is there a better way of writing this? i am using matlab.
> thanks
>
> d(1) = fi(2) - a*fi(1);
> d(79) = fi(80) - c*fi(81);
> for i = 3:79
> d(i) = fi(i-1)
> end
>
>
> where a and c are scalars.

How about d(3:79) = fi(2:78).

But why are you setting d(79) in the for loop and in the previous line as well?

Subject: how to vectorize this in matlab

From: matlab_learner

Date: 19 Mar, 2010 01:05:26

Message: 3 of 3

On Mar 18, 5:31 pm, "William Smith" <w...@commoditymodels.com> wrote:
> matlab_learner <cib...@gmail.com> wrote in message <91bf19f3-7065-4fe4-a6e5-1b7395c3e...@w27g2000pre.googlegroups.com>...
> > hi all
> > i want to eliminate the for statement...but the boundaries need to be
> > fixed. is there a better way of writing this? i am using matlab.
> > thanks
>
> >     d(1) = fi(2) - a*fi(1);
> >     d(79) = fi(80) - c*fi(81);
> >     for i = 3:79
> >         d(i) = fi(i-1)
> >     end
>
> > where a and c are scalars.
>
> How about d(3:79) = fi(2:78).
>
> But why are you setting d(79) in the for loop and in the previous line as well?

The problem is best solved in C. Using Matlab I am forced to shift my
index by 1

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com