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:
Function without loop

Subject: Function without loop

From: fas

Date: 28 Jan, 2009 06:42:12

Message: 1 of 5

I have written my objective function. However I have to run a loop. I
am wondering if I can write it without a loop. Here X is a 3x1000
matrix and Y is a scalar. In each loop it takes X(3,1) and create one
equation.

function f = objfun(x,X,Y)

for i=1:1000
    f=(X(:,i)*X(:,i)')*[x(1),x(2),x(3)]'-Y(i)*X(:,i);
end
f=norm(f);

Subject: Function without loop

From: Jos

Date: 28 Jan, 2009 09:22:01

Message: 2 of 5

fas <faisalmufti@gmail.com> wrote in message <3e334842-e716-484b-bba8-8aca2de0b028@a12g2000pro.googlegroups.com>...
> I have written my objective function. However I have to run a loop. I
> am wondering if I can write it without a loop. Here X is a 3x1000
> matrix and Y is a scalar. In each loop it takes X(3,1) and create one
> equation.
>
> function f = objfun(x,X,Y)
>
> for i=1:1000
> f=(X(:,i)*X(:,i)')*[x(1),x(2),x(3)]'-Y(i)*X(:,i);
> end
> f=norm(f);

All this doesn't make any sense.
If Y is really a scalar the loop will error for i=2, when refering to Y(2) ?
What do you mean "In each loop it takes X(3,1) ..." ?
f is continuously overwritten, so "f = X(:,1000) * X(:,1000)' * ..." would do as well.
What is x?

So, please restate your problem and code.

Jos

Subject: Function without loop

From: Nitin

Date: 28 Jan, 2009 11:13:38

Message: 3 of 5

On Jan 28, 11:42 am, fas <faisalmu...@gmail.com> wrote:
> I have written my objective function. However I have to run a loop. I
> am wondering if I can write it without a loop. Here X is a 3x1000
> matrix and Y is a scalar. In each loop it takes X(3,1) and create one
> equation.
>
> function f = objfun(x,X,Y)
>
> for i=1:1000
> f=(X(:,i)*X(:,i)')*[x(1),x(2),x(3)]'-Y(i)*X(:,i);
> end
> f=norm(f);

did you mean that Y is a "vector"? and, it takes "x(3,1)" in each
loop?

shouldn't your equation look like:
f(i)=(X(:,i)*X(:,i)')*[x(1),x(2),x(3)]'-Y(i)*X(:,i);


Nitin
--
nitin@idearesearch.in
www.idearesearch.in

Subject: Function without loop

From: fas

Date: 29 Jan, 2009 00:47:15

Message: 4 of 5

On Jan 28, 10:13=A0pm, Nitin <nitin.k.gu...@gmail.com> wrote:
> On Jan 28, 11:42 am, fas <faisalmu...@gmail.com> wrote:
>
> > I have written my objective function. However I have to run a loop. I
> > am wondering if I can write it without a loop. Here X is a 3x1000
> > matrix and Y is a scalar. In each loop it takes X(3,1) and create one
> > equation.
>
> > function f =3D objfun(x,X,Y)
>
> > for i=3D1:1000
> > =A0 =A0 f=3D(X(:,i)*X(:,i)')*[x(1),x(2),x(3)]'-Y(i)*X(:,i);
> > end
> > f=3Dnorm(f);
>
> did you mean that Y is a "vector"? and, it takes "x(3,1)" in each
> loop?
>
> shouldn't your equation look like:
> f(i)=3D(X(:,i)*X(:,i)')*[x(1),x(2),x(3)]'-Y(i)*X(:,i);
>
> Nitin
> --
> ni...@idearesearch.inwww.idearesearch.in

Sorry I made a mistake it should have been like that
f(:,i)=3D(X(:,i)*X(:,i)')*[x(1),x(2),x(3)]'-Y(i)*X(:,i);

and Y is a 1000x1 vector

Subject: Function without loop

From: Matt Fig

Date: 29 Jan, 2009 05:12:01

Message: 5 of 5

Simply pre-allocating f before the loop cuts run time in half.




)zf;5okdzdozpic\\`\zp^t\`hhojcjgz@z"`jTkc"]z^qz\`b\gj\j]Hji

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