From: SteveR R on
Hi I;m trying to do the following thing in an m file:

L1=inline('exp(-x/2);');
L2=inline('exp(-x/2)*(1-x);');
L3=inline('exp(-x/2)*(1-2*x+x*x/2);');

for i=1:3
a(i)=Li(10);
end


where a is a vector defined previousl.

I want it to loop going: a(1)=L1(10), a(2)=L2(10), a(3)=L3(10) but it doesn't like the Li in the loop.

Any suggestions apart from the obvious "do it without a loop", because this is just a simple example of what I actually need to do.

Cheers
From: John O'Flaherty on
On Sat, 15 May 2010 20:51:06 +0000 (UTC), "SteveR R"
<whatever(a)whatever.com> wrote:

>Hi I;m trying to do the following thing in an m file:
>
>L1=inline('exp(-x/2);');
>L2=inline('exp(-x/2)*(1-x);');
>L3=inline('exp(-x/2)*(1-2*x+x*x/2);');
>
>for i=1:3
> a(i)=Li(10);
>end
>
>
>where a is a vector defined previousl.
>
>I want it to loop going: a(1)=L1(10), a(2)=L2(10), a(3)=L3(10) but it doesn't like the Li in the loop.
>
>Any suggestions apart from the obvious "do it without a loop", because this is just a simple example of what I actually need to do.

It doesn't understand that you want L1 to be a numbered variable
rather than an arbitrary name, any more than it would understand Lone,
Ltwo, Lthree. You'll have to make it a vector L(1,10), L(2,10),
L(3,10).
--
john
From: us on
"SteveR R" <whatever(a)whatever.com> wrote in message <hsn1fq$k28$1(a)fred.mathworks.com>...
> Hi I;m trying to do the following thing in an m file:
>
> L1=inline('exp(-x/2);');
> L2=inline('exp(-x/2)*(1-x);');
> L3=inline('exp(-x/2)*(1-2*x+x*x/2);');
>
> for i=1:3
> a(i)=Li(10);
> end
>
>
> where a is a vector defined previousl.
>
> I want it to loop going: a(1)=L1(10), a(2)=L2(10), a(3)=L3(10) but it doesn't like the Li in the loop.
>
> Any suggestions apart from the obvious "do it without a loop", because this is just a simple example of what I actually need to do.
>
> Cheers

one of the many solutions
- you should consider to use function handles rather than (slower) INLINEs...

fh={ % <- create a function bank...
inline('exp(-x/2);')
inline('exp(-x/2)*(1-x);')
inline('exp(-x/2)*(1-2*x+x*x/2);')
};
a=nan(size(fh,1),1);
for i=1:size(fh,1)
a(i,1)=fh{i}(10);
end
disp(a);
%{
0.0067379
-0.060642
0.20888
%}

us
From: SteveR R on
Thanks for your replies. I eventually need to start creating matrices based on a user inputted number, is there nothing I can do in Matlab which would resembe the C++ code:
int main()
{
for (int i=0;i<10;i++)
{
int Si=i;
cout << Si << endl;
}

return 0;

}
This is what I was trying to show with my first example.

Cheers
Stevie

"us " <us(a)neurol.unizh.ch> wrote in message <hsn2jl$djm$1(a)fred.mathworks.com>...
> "SteveR R" <whatever(a)whatever.com> wrote in message <hsn1fq$k28$1(a)fred.mathworks.com>...
> > Hi I;m trying to do the following thing in an m file:
> >
> > L1=inline('exp(-x/2);');
> > L2=inline('exp(-x/2)*(1-x);');
> > L3=inline('exp(-x/2)*(1-2*x+x*x/2);');
> >
> > for i=1:3
> > a(i)=Li(10);
> > end
> >
> >
> > where a is a vector defined previousl.
> >
> > I want it to loop going: a(1)=L1(10), a(2)=L2(10), a(3)=L3(10) but it doesn't like the Li in the loop.
> >
> > Any suggestions apart from the obvious "do it without a loop", because this is just a simple example of what I actually need to do.
> >
> > Cheers
>
> one of the many solutions
> - you should consider to use function handles rather than (slower) INLINEs...
>
> fh={ % <- create a function bank...
> inline('exp(-x/2);')
> inline('exp(-x/2)*(1-x);')
> inline('exp(-x/2)*(1-2*x+x*x/2);')
> };
> a=nan(size(fh,1),1);
> for i=1:size(fh,1)
> a(i,1)=fh{i}(10);
> end
> disp(a);
> %{
> 0.0067379
> -0.060642
> 0.20888
> %}
>
> us
From: tinne123 on
Hey Steve R,

maybe you have good reasons why you want to run a loop over variable names. Being not any programmer myself, I have to say that I did best when I followed the advise of some forum users and avoided that sort of approach.

In general, there are a few lines on creating variable names in a loop at
http://matlabwiki.mathworks.com/MATLAB_FAQ
under 4.6. However, following the suggestion of us is probably the best way to go, based on own experience.