<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264073</link>
    <title>MATLAB Central Newsreader - Beginner's loop</title>
    <description>Feed for thread: Beginner's loop</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Sun, 25 Oct 2009 20:48:29 -0400</pubDate>
      <title>Beginner's loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264073#689610</link>
      <author>Matlab Beginner</author>
      <description>hello everybody.&lt;br&gt;
&lt;br&gt;
I am a beginner in matlab and I encounter some problems when&lt;br&gt;
programming loops. I would like to understand what I do wrong and how&lt;br&gt;
can I make it better. I hope there somebody can help me with this.&lt;br&gt;
&lt;br&gt;
the problem is the following:&lt;br&gt;
&lt;br&gt;
I need to write a function with branches that I need to use afterward&lt;br&gt;
in computing some optimal values.&lt;br&gt;
1. I wrote first a function file for the branching function:&lt;br&gt;
&lt;br&gt;
function [c] = c1(x);&lt;br&gt;
&lt;br&gt;
for i = 1:size(x)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if x(i)&amp;gt;=0.5&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c(i) = (x(i)&amp;gt;=0.5)^2;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;c(i) = x(i)-1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
in principle I want it to go through all the elements of a vector that&lt;br&gt;
I will give in later and evaluate it using the resp. expression.&lt;br&gt;
&lt;br&gt;
2.Testing the function first: when In the main file I write:&lt;br&gt;
&lt;br&gt;
x=linspace(-1,10,100)&lt;br&gt;
&lt;br&gt;
c1(x)...&lt;br&gt;
&lt;br&gt;
it evaluates only x(1)=-1... what am I doing wrong?&lt;br&gt;
&lt;br&gt;
3. What I actually need is to use the function in this function file:&lt;br&gt;
(e is a vector of variables e(1) and e(2))&lt;br&gt;
&lt;br&gt;
function [firstordcond1, firstordcond2] = nest1seller(e,el,g,mu,b,Bet)&lt;br&gt;
&lt;br&gt;
firstordcond2 = c1(e)-((Bet*b*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)&lt;br&gt;
*b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));&lt;br&gt;
firstordcond1 = c1(2)-((Bet*g*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)&lt;br&gt;
*b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
could anybody be so kind and help me with this thing?&lt;br&gt;
&lt;br&gt;
many many thanks&lt;br&gt;
a matlab beginner</description>
    </item>
    <item>
      <pubDate>Mon, 26 Oct 2009 01:24:01 -0400</pubDate>
      <title>Re: Beginner's loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264073#689642</link>
      <author>Phil Goddard</author>
      <description>Given your input vector,&lt;br&gt;
&amp;gt;&amp;gt; for i = 1:size(x)&lt;br&gt;
is equivalent to&lt;br&gt;
&amp;gt;&amp;gt; for i = 1:[1 100]&lt;br&gt;
which is equivalent to&lt;br&gt;
&amp;gt;&amp;gt; for i = 1:1&lt;br&gt;
&lt;br&gt;
Hence you are only getting one pass through the loop.&lt;br&gt;
&lt;br&gt;
Assuming x is always a vector you want&lt;br&gt;
&amp;gt;&amp;gt; for i = 1:length(x)&lt;br&gt;
&lt;br&gt;
Phil.</description>
    </item>
    <item>
      <pubDate>Mon, 26 Oct 2009 03:47:21 -0400</pubDate>
      <title>Re: Beginner's loop</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264073#689660</link>
      <author>NZTideMan</author>
      <description>On Oct 26, 9:48&#160;am, Matlab Beginner &amp;lt;matlabegin...@googlemail.com&amp;gt;&lt;br&gt;
wrote:&lt;br&gt;
&amp;gt; hello everybody.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am a beginner in matlab and I encounter some problems when&lt;br&gt;
&amp;gt; programming loops. I would like to understand what I do wrong and how&lt;br&gt;
&amp;gt; can I make it better. I hope there somebody can help me with this.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; the problem is the following:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I need to write a function with branches that I need to use afterward&lt;br&gt;
&amp;gt; in computing some optimal values.&lt;br&gt;
&amp;gt; 1. I wrote first a function file for the branching function:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; function [c] = c1(x);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; for i = 1:size(x)&lt;br&gt;
&amp;gt; &#160; &#160; if x(i)&amp;gt;=0.5&lt;br&gt;
&amp;gt; &#160; &#160; &#160; &#160; c(i) = (x(i)&amp;gt;=0.5)^2;&lt;br&gt;
&amp;gt; &#160; &#160; else&lt;br&gt;
&amp;gt; &#160; &#160; &#160; &#160; c(i) = x(i)-1;&lt;br&gt;
&amp;gt; &#160; &#160; end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; in principle I want it to go through all the elements of a vector that&lt;br&gt;
&amp;gt; I will give in later and evaluate it using the resp. expression.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; 2.Testing the function first: when In the main file I write:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; x=linspace(-1,10,100)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; c1(x)...&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; it evaluates only x(1)=-1... what am I doing wrong?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; 3. What I actually need is to use the function in this function file:&lt;br&gt;
&amp;gt; (e is a vector of variables e(1) and e(2))&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; function [firstordcond1, firstordcond2] = nest1seller(e,el,g,mu,b,Bet)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; firstordcond2 = c1(e)-((Bet*b*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)&lt;br&gt;
&amp;gt; *b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));&lt;br&gt;
&amp;gt; firstordcond1 = c1(2)-((Bet*g*el*(g-b)*((mu*g*e(1))./(mu*g*e(1)+(1-mu)&lt;br&gt;
&amp;gt; *b*e(2))-(mu*(1-g*e(1)))./(mu*(1-g*e(1))+(1-mu)*(1-b*e(2))))));&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; could anybody be so kind and help me with this thing?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; many many thanks&lt;br&gt;
&amp;gt; a matlab beginner&lt;br&gt;
&lt;br&gt;
The great thing about Matlab is that expressions can be vectorised.&lt;br&gt;
This means that for many cases, you don't need a loop at all.&lt;br&gt;
For your case, you can do it like this:&lt;br&gt;
c=x-1;   % this creates a vector c with each element being x-1&lt;br&gt;
c(x&amp;gt;=0.5)=(x&amp;gt;=0.5).^2;    % Changes just the elements of c where x&lt;br&gt;
&amp;gt;=0.5&lt;br&gt;
Note: I have written this on the RHS:&lt;br&gt;
(x&amp;gt;=0.5).^2;&lt;br&gt;
because it is equivalent to what you have written, but it is nonsense.&lt;br&gt;
Its value is always unity because x&amp;gt;=0.5 is logical 1 and when you&lt;br&gt;
square it you get 1.&lt;br&gt;
Maybe you meant:&lt;br&gt;
(x-0.5).^2;&lt;br&gt;
but who knows?&lt;br&gt;
&lt;br&gt;
I glanced at your question 3, but its impenetrable.  You'll have to&lt;br&gt;
simplify it.</description>
    </item>
  </channel>
</rss>

