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:
Vectorize a for loop

Subject: Vectorize a for loop

From: Hugh

Date: 25 Sep, 2012 21:41:07

Message: 1 of 2

Hi there,

Can I vectorise this for loop?

F=zeros(de.nP,1);
  
 for i=1:de.nP
   
    F(i)=OF(P1(:,i)',data);
    
 end

function ObjVal=OF(beta, data)
mats=data.mats;
yM=data.yM;
model=data.model;
y=model(beta,mats);
aux=y-yM;
ObjVal=dot(aux,aux);
end

When I simply try to do itlike this:

F=OF(P1,data);

It passes all of P1 at th same time but I want it to just pass one column of P1 at a time?

Subject: Vectorize a for loop

From: Ross W

Date: 29 Sep, 2012 23:13:06

Message: 2 of 2

"Hugh" wrote in message <k3t8dj$861$1@newscl01ah.mathworks.com>...
> Hi there,
>
> Can I vectorise this for loop?
>
> F=zeros(de.nP,1);
>
> for i=1:de.nP
>
> F(i)=OF(P1(:,i)',data);
>
> end
>
> function ObjVal=OF(beta, data)
> mats=data.mats;
> yM=data.yM;
> model=data.model;
> y=model(beta,mats);
> aux=y-yM;
> ObjVal=dot(aux,aux);
> end
>
> When I simply try to do itlike this:
>
> F=OF(P1,data);
>
> It passes all of P1 at th same time but I want it to just pass one column of P1 at a time?

I think the answer depends on what is happening in the line
> y=model(beta,mats);

Does the "model" function accept a matrix of beta vectors, or just one vector at a time?

Cheers,
Ross

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