<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168262</link>
    <title>MATLAB Central Newsreader - simple calculation - where am I wrong?</title>
    <description>Feed for thread: simple calculation - where am I wrong?</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, 25 Apr 2008 12:12:02 -0400</pubDate>
      <title>simple calculation - where am I wrong?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168262#428815</link>
      <author>N N</author>
      <description>Could anyone help with the following calculation plse? &lt;br&gt;
&lt;br&gt;
&lt;br&gt;
a = [2 4 20 3 6]; &lt;br&gt;
%number of attempts at each level, total 5 levels; for &lt;br&gt;
example, first level was attempted 2 times&lt;br&gt;
&lt;br&gt;
b= [1 .9 .5 0 1]; &lt;br&gt;
% percent correct at each level; for example, first level &lt;br&gt;
was correct 100 percent times &lt;br&gt;
&amp;nbsp;&lt;br&gt;
for n=1:length(a);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for m=1:length(b);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if b(m) == 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b(m) = 1/(2.*a(n));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;elseif b(m) == 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b(m) = 1-(1/(2.*a(n)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b(m) = b(m);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
b&lt;br&gt;
&lt;br&gt;
My aim is to get &quot;b&quot; without any 1s and 0s. Replaced &lt;br&gt;
numbers totally depend on number of attempts (higher the &lt;br&gt;
number of attempts, closer the &quot;b&quot; will be to &quot;one&quot; &lt;br&gt;
or &quot;zero&quot;.&lt;br&gt;
&lt;br&gt;
With the above code, all the replaced numbers have been &lt;br&gt;
calculated based on 1st element of &amp;#8220;a&amp;#8221; (think my code is &lt;br&gt;
incorrect somewhere) &lt;br&gt;
&lt;br&gt;
Current answer is the following:&lt;br&gt;
b =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.75    0.90    0.50    0.25    0.75 &lt;br&gt;
(In this answer, last 2 calculations are incorrect)&lt;br&gt;
&lt;br&gt;
Expected answer:&lt;br&gt;
b =&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.75    0.90    0.50    0.17    0.92&lt;br&gt;
&lt;br&gt;
Help is much appreciated. Thanks.</description>
    </item>
    <item>
      <pubDate>Fri, 25 Apr 2008 12:33:02 -0400</pubDate>
      <title>Re: simple calculation - where am I wrong?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168262#428821</link>
      <author>david szotten</author>
      <description>i may have misunderstood, but it seems you only want to loop&lt;br&gt;
through once.&lt;br&gt;
&lt;br&gt;
if you remove the outer loop and replace references to n, by&lt;br&gt;
m, you get the answer you wanted&lt;br&gt;
&lt;br&gt;
a = [2 4 20 3 6];&lt;br&gt;
b= [1 .9 .5 0 1]; &lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for m=1:length(b);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if b(m) == 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b(m) = 1/(2.*a(m));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;elseif b(m) == 1&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b(m) = 1-(1/(2.*a(m)));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&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;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;b(m) = b(m);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
b =&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.7500    0.9000    0.5000    0.1667    0.9167&lt;br&gt;
&lt;br&gt;
regards,&lt;br&gt;
david&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;N N&quot; &amp;lt;cvlm_2005@hotmail.com&amp;gt; wrote in message&lt;br&gt;
&amp;lt;fushqi$j4c$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Could anyone help with the following calculation plse? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; a = [2 4 20 3 6]; &lt;br&gt;
&amp;gt; %number of attempts at each level, total 5 levels; for &lt;br&gt;
&amp;gt; example, first level was attempted 2 times&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; b= [1 .9 .5 0 1]; &lt;br&gt;
&amp;gt; % percent correct at each level; for example, first level &lt;br&gt;
&amp;gt; was correct 100 percent times &lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt; for n=1:length(a);&lt;br&gt;
&amp;gt;     for m=1:length(b);&lt;br&gt;
&amp;gt;         if b(m) == 0&lt;br&gt;
&amp;gt;             b(m) = 1/(2.*a(n));&lt;br&gt;
&amp;gt;         elseif b(m) == 1&lt;br&gt;
&amp;gt;             b(m) = 1-(1/(2.*a(n)));&lt;br&gt;
&amp;gt;         else&lt;br&gt;
&amp;gt;             b(m) = b(m);&lt;br&gt;
&amp;gt;         end&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; b&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My aim is to get &quot;b&quot; without any 1s and 0s. Replaced &lt;br&gt;
&amp;gt; numbers totally depend on number of attempts (higher the &lt;br&gt;
&amp;gt; number of attempts, closer the &quot;b&quot; will be to &quot;one&quot; &lt;br&gt;
&amp;gt; or &quot;zero&quot;.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; With the above code, all the replaced numbers have been &lt;br&gt;
&amp;gt; calculated based on 1st element of &amp;#8220;a&amp;#8221; (think my code is &lt;br&gt;
&amp;gt; incorrect somewhere) &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Current answer is the following:&lt;br&gt;
&amp;gt; b =&lt;br&gt;
&amp;gt;     0.75    0.90    0.50    0.25    0.75 &lt;br&gt;
&amp;gt; (In this answer, last 2 calculations are incorrect)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Expected answer:&lt;br&gt;
&amp;gt; b =&lt;br&gt;
&amp;gt;     0.75    0.90    0.50    0.17    0.92&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Help is much appreciated. Thanks.&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Fri, 25 Apr 2008 13:26:11 -0400</pubDate>
      <title>Re: simple calculation - where am I wrong?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168262#428845</link>
      <author>Steven Lord</author>
      <description>&lt;br&gt;
&quot;N N&quot; &amp;lt;cvlm_2005@hotmail.com&amp;gt; wrote in message &lt;br&gt;
news:fushqi$j4c$1@fred.mathworks.com...&lt;br&gt;
&amp;gt; Could anyone help with the following calculation plse?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; a = [2 4 20 3 6];&lt;br&gt;
&amp;gt; %number of attempts at each level, total 5 levels; for&lt;br&gt;
&amp;gt; example, first level was attempted 2 times&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; b= [1 .9 .5 0 1];&lt;br&gt;
&amp;gt; % percent correct at each level; for example, first level&lt;br&gt;
&amp;gt; was correct 100 percent times&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; for n=1:length(a);&lt;br&gt;
&amp;gt;    for m=1:length(b);&lt;br&gt;
&amp;gt;        if b(m) == 0&lt;br&gt;
&amp;gt;            b(m) = 1/(2.*a(n));&lt;br&gt;
&lt;br&gt;
At the first iteration through the N loop, if b(m) is equal to 0, you're &lt;br&gt;
replacing it with 1/(2*a(1)).  Unless a(1) is 0.5 or Inf, this means that &lt;br&gt;
b(m) will not be 0 or 1 in any further iterations of your N loop, and so &lt;br&gt;
will never be replaced again.&lt;br&gt;
&lt;br&gt;
&amp;gt;        elseif b(m) == 1&lt;br&gt;
&amp;gt;            b(m) = 1-(1/(2.*a(n)));&lt;br&gt;
&lt;br&gt;
Similarly, unless a(1) is 0.5 or Inf, this will replace b(m) with a value &lt;br&gt;
other than 0 or 1 in the first iteration of your N loop, so no further &lt;br&gt;
iterations through that loop will modify this b(m).&lt;br&gt;
&lt;br&gt;
&amp;gt;        else&lt;br&gt;
&amp;gt;            b(m) = b(m);&lt;br&gt;
&lt;br&gt;
You technically don't need this line, but I don't think it's hurting &lt;br&gt;
anything.&lt;br&gt;
&lt;br&gt;
&amp;gt;        end&lt;br&gt;
&amp;gt;    end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; b&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; My aim is to get &quot;b&quot; without any 1s and 0s. Replaced&lt;br&gt;
&amp;gt; numbers totally depend on number of attempts (higher the&lt;br&gt;
&amp;gt; number of attempts, closer the &quot;b&quot; will be to &quot;one&quot;&lt;br&gt;
&amp;gt; or &quot;zero&quot;.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; With the above code, all the replaced numbers have been&lt;br&gt;
&amp;gt; calculated based on 1st element of &amp;#8220;a&amp;#8221; (think my code is&lt;br&gt;
&amp;gt; incorrect somewhere)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Current answer is the following:&lt;br&gt;
&amp;gt; b =&lt;br&gt;
&amp;gt;    0.75    0.90    0.50    0.25    0.75&lt;br&gt;
&amp;gt; (In this answer, last 2 calculations are incorrect)&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Expected answer:&lt;br&gt;
&amp;gt; b =&lt;br&gt;
&amp;gt;    0.75    0.90    0.50    0.17    0.92&lt;br&gt;
&lt;br&gt;
Let me restate your aim, to make sure I understand.  You want to replace &lt;br&gt;
those elements of b that are equal to 0 with 1 divided by 2 times the &lt;br&gt;
corresponding element of a.  You also want to replace those elements of b &lt;br&gt;
that are equal to 1 with 1 minus 1 divided by 2 times the corresponding &lt;br&gt;
element of a.  Correct?&lt;br&gt;
&lt;br&gt;
If so, use logical indexing.&lt;br&gt;
&lt;br&gt;
% Data&lt;br&gt;
a = [2 4 20 3 6];&lt;br&gt;
b= [1 .9 .5 0 1];&lt;br&gt;
&lt;br&gt;
% Handle the b equal to 0 case&lt;br&gt;
% locationOfZeros will be a logical array that we will use as a &quot;mask&quot; on a &lt;br&gt;
and b&lt;br&gt;
locationOfZeros = (b == 0);&lt;br&gt;
&lt;br&gt;
% Now replace the elements of b corresponding to 1's in locationOfZeros&lt;br&gt;
%&lt;br&gt;
% Note I'm using locationOfZeros to index into a as well, to get the &lt;br&gt;
elements&lt;br&gt;
% of a corresponding to 0's in b.  I also need to change 1/ to 1./ to &lt;br&gt;
perform&lt;br&gt;
% element-by-element division rather than matrix division.&lt;br&gt;
%&lt;br&gt;
% I don't need to use .* instead of * because 2 is a scalar, and matrix &lt;br&gt;
multiplying&lt;br&gt;
% with a scalar is the same as element-by-element multiplication.  I could &lt;br&gt;
use .*&lt;br&gt;
% if I wanted to, though.&lt;br&gt;
b(locationOfZeros) = 1./(2*a(locationOfZeros));&lt;br&gt;
&lt;br&gt;
% Handle the b equal to 1 case similarly&lt;br&gt;
% Minus (-) is already an element-by-element operator, so there's no .-&lt;br&gt;
locationOfOnes = (b == 1);&lt;br&gt;
b(locationOfOnes) = 1-(1./(2*a(locationOfOnes)));&lt;br&gt;
&lt;br&gt;
The one case that would change between your code and my code is the a = 1/2 &lt;br&gt;
case, but since a is a number of trials, that shouldn't happen.&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
Steve Lord&lt;br&gt;
slord@mathworks.com </description>
    </item>
    <item>
      <pubDate>Mon, 28 Apr 2008 00:04:02 -0400</pubDate>
      <title>Re: simple calculation - where am I wrong?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/168262#429091</link>
      <author>N N</author>
      <description>Thank you for your responses. Cheers!</description>
    </item>
  </channel>
</rss>

