Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!w37g2000prg.googlegroups.com!not-for-mail
From: NZTideMan <mulgor@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Beginner's loop
Date: Sun, 25 Oct 2009 20:47:21 -0700 (PDT)
Organization: http://groups.google.com
Lines: 69
Message-ID: <27fb9667-bc71-4258-9d91-d0b0c2edd23d@w37g2000prg.googlegroups.com>
References: <d228bd22-aa0e-4bc6-9a52-1dfbb168aaf5@g31g2000vbr.googlegroups.com>
NNTP-Posting-Host: 202.78.152.105
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1256528841 4266 127.0.0.1 (26 Oct 2009 03:47:21 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Mon, 26 Oct 2009 03:47:21 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: w37g2000prg.googlegroups.com; posting-host=202.78.152.105; 
	posting-account=qPexFwkAAABOl8VUndE6Jm-9Z5z_fSpR
User-Agent: G2/1.0
X-HTTP-Via: 1.1 bc2
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) 
	Gecko/20090824 Firefox/3.5.3,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:579972


On Oct 26, 9:48 am, Matlab Beginner <matlabegin...@googlemail.com>
wrote:
> hello everybody.
>
> I am a beginner in matlab and I encounter some problems when
> programming loops. I would like to understand what I do wrong and how
> can I make it better. I hope there somebody can help me with this.
>
> the problem is the following:
>
> I need to write a function with branches that I need to use afterward
> in computing some optimal values.
> 1. I wrote first a function file for the branching function:
>
> function [c] = c1(x);
>
> for i = 1:size(x)
>     if x(i)>=0.5
>         c(i) = (x(i)>=0.5)^2;
>     else
>         c(i) = x(i)-1;
>     end
> end
>
> in principle I want it to go through all the elements of a vector that
> I will give in later and evaluate it using the resp. expression.
>
> 2.Testing the function first: when In the main file I write:
>
> x=linspace(-1,10,100)
>
> c1(x)...
>
> it evaluates only x(1)=-1... what am I doing wrong?
>
> 3. What I actually need is to use the function in this function file:
> (e is a vector of variables e(1) and e(2))
>
> function [firstordcond1, firstordcond2] = nest1seller(e,el,g,mu,b,Bet)
>
> firstordcond2 = c1(e)-((Bet*b*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)
> *b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));
> firstordcond1 = c1(2)-((Bet*g*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)
> *b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));
> end
>
> could anybody be so kind and help me with this thing?
>
> many many thanks
> a matlab beginner

The great thing about Matlab is that expressions can be vectorised.
This means that for many cases, you don't need a loop at all.
For your case, you can do it like this:
c=x-1;   % this creates a vector c with each element being x-1
c(x>=0.5)=(x>=0.5).^2;    % Changes just the elements of c where x
>=0.5
Note: I have written this on the RHS:
(x>=0.5).^2;
because it is equivalent to what you have written, but it is nonsense.
Its value is always unity because x>=0.5 is logical 1 and when you
square it you get 1.
Maybe you meant:
(x-0.5).^2;
but who knows?

I glanced at your question 3, but its impenetrable.  You'll have to
simplify it.