<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237304</link>
    <title>MATLAB Central Newsreader - if statements</title>
    <description>Feed for thread: if statements</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>Fri, 10 Oct 2008 03:31:02 -0400</pubDate>
      <title>if statements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237304#604577</link>
      <author>Ongun Palaoglu</author>
      <description>hello guys i am trying to use the if command,&lt;br&gt;
&amp;nbsp;my code:&lt;br&gt;
&lt;br&gt;
%setting parameter&lt;br&gt;
vtn = 0.75;&lt;br&gt;
vds = [0.2 2.5 0];&lt;br&gt;
vgs = [2 2 0];&lt;br&gt;
knp=[200*10^-6 300*10^-6];&lt;br&gt;
kp=knp*10;&lt;br&gt;
i = 1; len = length (vgs);&lt;br&gt;
while i &amp;lt;= len&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;i = i + 1;&lt;br&gt;
end&lt;br&gt;
while&lt;br&gt;
id=kp.'*x&lt;br&gt;
if id == 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('cutoff')&lt;br&gt;
else &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('saturation')&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
this is just an exercise i created. what is teh mistake in here. i want to display the cutoff value and saturation value. for example; 2 saturation.&lt;br&gt;
&lt;br&gt;
and I also want to improve this code, i want to use saturation values on a different function.&lt;br&gt;
&lt;br&gt;
thanks a ot.</description>
    </item>
    <item>
      <pubDate>Fri, 10 Oct 2008 09:15:19 -0400</pubDate>
      <title>Re: if statements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237304#604610</link>
      <author>Jos </author>
      <description>&quot;Ongun Palaoglu&quot; &amp;lt;ongun@mac.com&amp;gt; wrote in message &amp;lt;gcmi9m$lbj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; hello guys i am trying to use the if command,&lt;br&gt;
&amp;gt;  my code:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; %setting parameter&lt;br&gt;
&amp;gt; vtn = 0.75;&lt;br&gt;
&amp;gt; vds = [0.2 2.5 0];&lt;br&gt;
&amp;gt; vgs = [2 2 0];&lt;br&gt;
&amp;gt; knp=[200*10^-6 300*10^-6];&lt;br&gt;
&amp;gt; kp=knp*10;&lt;br&gt;
&amp;gt; i = 1; len = length (vgs);&lt;br&gt;
&lt;br&gt;
Since this loop executes a fixed number of times, you should change it into a for-loop&lt;br&gt;
for i=1:len,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;x(i) =&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
which you can vectorize using the dot notation (.*)&lt;br&gt;
x = vds .* (vgs - vtn - vds/2);&lt;br&gt;
&lt;br&gt;
&amp;gt; while i &amp;lt;= len&lt;br&gt;
&amp;gt;     x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);&lt;br&gt;
&amp;gt;     i = i + 1;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
I am pretty sure this &quot;while&quot; does not belong here:&lt;br&gt;
&lt;br&gt;
&amp;gt; while&lt;br&gt;
&lt;br&gt;
In general, it is a bad idea to compare floating point numbers exactly. Try abs(id)&amp;lt; 0.00001&lt;br&gt;
&lt;br&gt;
&amp;gt; id=kp.'*x&lt;br&gt;
&amp;gt; if id == 0&lt;br&gt;
&amp;gt;     disp('cutoff')&lt;br&gt;
&amp;gt; else &lt;br&gt;
&amp;gt;     disp('saturation')&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; this is just an exercise i created. what is teh mistake in here. i want to display the cutoff value and saturation value. for example; 2 saturation.&lt;br&gt;
&lt;br&gt;
To show the number, use for instance&lt;br&gt;
disp(id)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and I also want to improve this code, i want to use saturation values on a different function.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; thanks a ot.&lt;br&gt;
&lt;br&gt;
hth&lt;br&gt;
Jos</description>
    </item>
    <item>
      <pubDate>Fri, 10 Oct 2008 13:45:54 -0400</pubDate>
      <title>Re: if statements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237304#604640</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;Jos &quot; &amp;lt;DELjos@jasenDEL.nl&amp;gt; wrote in message &lt;br&gt;
news:gcn6f7$p95$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; &quot;Ongun Palaoglu&quot; &amp;lt;ongun@mac.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;lt;gcmi9m$lbj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&amp;gt; hello guys i am trying to use the if command,&lt;br&gt;
&amp;gt;&amp;gt;  my code:&lt;br&gt;
&amp;gt;&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; %setting parameter&lt;br&gt;
&amp;gt;&amp;gt; vtn = 0.75;&lt;br&gt;
&amp;gt;&amp;gt; vds = [0.2 2.5 0];&lt;br&gt;
&amp;gt;&amp;gt; vgs = [2 2 0];&lt;br&gt;
&amp;gt;&amp;gt; knp=[200*10^-6 300*10^-6];&lt;br&gt;
&amp;gt;&amp;gt; kp=knp*10;&lt;br&gt;
&amp;gt;&amp;gt; i = 1; len = length (vgs);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Since this loop executes a fixed number of times, you should change it &lt;br&gt;
&amp;gt; into a for-loop&lt;br&gt;
&amp;gt; for i=1:len,&lt;br&gt;
&amp;gt;   x(i) =&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; which you can vectorize using the dot notation (.*)&lt;br&gt;
&amp;gt; x = vds .* (vgs - vtn - vds/2);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; while i &amp;lt;= len&lt;br&gt;
&amp;gt;&amp;gt;     x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);&lt;br&gt;
&amp;gt;&amp;gt;     i = i + 1;&lt;br&gt;
&amp;gt;&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I am pretty sure this &quot;while&quot; does not belong here:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; while&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; In general, it is a bad idea to compare floating point numbers exactly. &lt;br&gt;
&amp;gt; Try abs(id)&amp;lt; 0.00001&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt; id=kp.'*x&lt;br&gt;
&amp;gt;&amp;gt; if id == 0&lt;br&gt;
&lt;br&gt;
In addition to Jos's comments, this line may not do what you think it will &lt;br&gt;
do.  From HELP IF:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;The statements are executed if the real part of the expression&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;has all non-zero elements.&lt;br&gt;
&lt;br&gt;
So unless ALL of the elements of id are equal to 0, the IF condition will &lt;br&gt;
never be satisfied.  If you want to satisfy the IF condition if ANY of the &lt;br&gt;
elements of id are equal to 0, you'd want:&lt;br&gt;
&lt;br&gt;
if any(id == 0)&lt;br&gt;
&lt;br&gt;
or, to account for floating-point as Jos said,&lt;br&gt;
&lt;br&gt;
if any(abs(id) &amp;lt; 1e-5)&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
    <item>
      <pubDate>Fri, 10 Oct 2008 18:54:02 -0400</pubDate>
      <title>Re: if statements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237304#604692</link>
      <author>Ongun Palaoglu</author>
      <description>&quot;Steven Lord&quot; &amp;lt;slord@mathworks.com&amp;gt; wrote in message &amp;lt;gcnmai$4d$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &quot;Jos &quot; &amp;lt;DELjos@jasenDEL.nl&amp;gt; wrote in message &lt;br&gt;
&amp;gt; news:gcn6f7$p95$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; &amp;gt; &quot;Ongun Palaoglu&quot; &amp;lt;ongun@mac.com&amp;gt; wrote in message &lt;br&gt;
&amp;gt; &amp;gt; &amp;lt;gcmi9m$lbj$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; hello guys i am trying to use the if command,&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;  my code:&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; %setting parameter&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; vtn = 0.75;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; vds = [0.2 2.5 0];&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; vgs = [2 2 0];&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; knp=[200*10^-6 300*10^-6];&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; kp=knp*10;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; i = 1; len = length (vgs);&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; Since this loop executes a fixed number of times, you should change it &lt;br&gt;
&amp;gt; &amp;gt; into a for-loop&lt;br&gt;
&amp;gt; &amp;gt; for i=1:len,&lt;br&gt;
&amp;gt; &amp;gt;   x(i) =&lt;br&gt;
&amp;gt; &amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; which you can vectorize using the dot notation (.*)&lt;br&gt;
&amp;gt; &amp;gt; x = vds .* (vgs - vtn - vds/2);&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; while i &amp;lt;= len&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;     x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;     i = i + 1;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; end&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; I am pretty sure this &quot;while&quot; does not belong here:&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; while&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt; In general, it is a bad idea to compare floating point numbers exactly. &lt;br&gt;
&amp;gt; &amp;gt; Try abs(id)&amp;lt; 0.00001&lt;br&gt;
&amp;gt; &amp;gt;&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; id=kp.'*x&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; if id == 0&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In addition to Jos's comments, this line may not do what you think it will &lt;br&gt;
&amp;gt; do.  From HELP IF:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;     The statements are executed if the real part of the expression&lt;br&gt;
&amp;gt;     has all non-zero elements.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; So unless ALL of the elements of id are equal to 0, the IF condition will &lt;br&gt;
&amp;gt; never be satisfied.  If you want to satisfy the IF condition if ANY of the &lt;br&gt;
&amp;gt; elements of id are equal to 0, you'd want:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if any(id == 0)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; or, to account for floating-point as Jos said,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; if any(abs(id) &amp;lt; 1e-5)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -- &lt;br&gt;
&amp;gt; Steve Lord&lt;br&gt;
&amp;gt; slord@mathworks.com &lt;br&gt;
&amp;gt; &lt;br&gt;
hey, &lt;br&gt;
&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; while i &amp;lt;= len&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;     x(i)=vds(i)*(vgs(i)-vtn-vds(i)/2);&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;     i = i + 1;&lt;br&gt;
&lt;br&gt;
this part works perfecly fine, when and he result is 3 by 2 matrix, and at the first row's first number is positive  and last 2 is 0. &lt;br&gt;
so i want to compare the values,i want to pull zeros out of the ans, and want to call them saturation, and i want to use them for another function to calculate another thing. and it is same for the first number.&lt;br&gt;
&lt;br&gt;
thats why I said &lt;br&gt;
if id=0&lt;br&gt;
disp('curoff')&lt;br&gt;
else&lt;br&gt;
disp('sat')&lt;br&gt;
end&lt;br&gt;
after this point i want to call the saturated values, for another function</description>
    </item>
    <item>
      <pubDate>Sat, 11 Oct 2008 06:36:06 -0400</pubDate>
      <title>Re: if statements</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/237304#604743</link>
      <author>swgillan</author>
      <description>First of all comments help identify your intention you are trying to&lt;br&gt;
do with you code.&lt;br&gt;
&amp;gt; =A0my code:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; %setting parameter&lt;br&gt;
&amp;gt; vtn =3D 0.75;&lt;br&gt;
&amp;gt; vds =3D [0.2 2.5 0];&lt;br&gt;
&amp;gt; vgs =3D [2 2 0];&lt;br&gt;
&amp;gt; knp=3D[200*10^-6 300*10^-6];&lt;br&gt;
&amp;gt; kp=3Dknp*10;&lt;br&gt;
&amp;gt; i =3D 1; len =3D length (vgs);&lt;br&gt;
&amp;gt; while i &amp;lt;=3D len&lt;br&gt;
&amp;gt; =A0 =A0 x(i)=3Dvds(i)*(vgs(i)-vtn-vds(i)/2);&lt;br&gt;
&amp;gt; =A0 =A0 i =3D i + 1;&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
This part of your code will not work as someone mentioned above&lt;br&gt;
because you are treating the vector id like a scalar. Also it looks&lt;br&gt;
like you are creating an infinite loop here as your while has not exit&lt;br&gt;
condition.&lt;br&gt;
&lt;br&gt;
&amp;gt; while&lt;br&gt;
&amp;gt; id=3Dkp.'*x&lt;br&gt;
&lt;br&gt;
&amp;gt; if id =3D=3D 0&lt;br&gt;
This like above will only be true of all the elements in id are zero.&lt;br&gt;
&amp;gt; =A0 =A0 disp('cutoff')&lt;br&gt;
&amp;gt; else&lt;br&gt;
&amp;gt; =A0 =A0 disp('saturation')&lt;br&gt;
&amp;gt; end&lt;br&gt;
&lt;br&gt;
This, to me, makes a lot more sense.&lt;br&gt;
&lt;br&gt;
id=3Dkp.*x;&lt;br&gt;
%Check to see if the value of id is saturated&lt;br&gt;
for i =3D1:len(id)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if id(i) =3D 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;disp('cutoff');&lt;br&gt;
&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;disp('saturated');&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Hope this helps.</description>
    </item>
  </channel>
</rss>

