<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/303796</link>
    <title>MATLAB Central Newsreader - simple array relational problem</title>
    <description>Feed for thread: simple array relational problem</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2013 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.nl/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 03 Mar 2011 06:40:21 +0000</pubDate>
      <title>simple array relational problem</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/303796#822810</link>
      <author>John Wong</author>
      <description>Given an array n is all integers from 1 to 30, and array y is given by two conditions.&lt;br&gt;
If n is divisible by 3,5,or 7, y is square root of n, otherwise, y is zero.&lt;br&gt;
&lt;br&gt;
I tested with if-statements and array relational method, but the latter did not agree with the correct result (the if-statements is right in this case).&lt;br&gt;
&lt;br&gt;
n = 1:30;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;y= (n.^(1/2)).*(mod(n,3)==0) + (n.^(1/2)).*(mod(n,5)==0) + (n.^(1/2)).*(mod(n,7)==0) + 0;&lt;br&gt;
sum(y)   % gives 79.3984&lt;br&gt;
&lt;br&gt;
for n = 1:30&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if(mod(n,3) == 0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(n) = n^(1/2);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if (mod(n,5)==0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(n) = n^(1/2);&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if(mod(n,7)==0)&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;y(n) = n^(1/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;y(n)= 0;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
&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;
sum(y)    % gives 65.4656&lt;br&gt;
&lt;br&gt;
I got the right answer by writing this little demo code&lt;br&gt;
% z = [ 3 5 6 7 9 10 12 14 15 18 20 21 24 25 27 28 30]&lt;br&gt;
% for n = 1:17&lt;br&gt;
%     g(n) = z(n).^(1/2);&lt;br&gt;
% end&lt;br&gt;
% sum(g)&lt;br&gt;
&lt;br&gt;
My question is, where is my mistake??? Can anyone spot one? &lt;br&gt;
&lt;br&gt;
Thank you!</description>
    </item>
    <item>
      <pubDate>Thu, 03 Mar 2011 07:15:22 +0000</pubDate>
      <title>Re: simple array relational problem</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/303796#822816</link>
      <author>Roger Stafford</author>
      <description>"John Wong" wrote in message &amp;lt;iknd4l$her$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Given an array n is all integers from 1 to 30, and array y is given by two conditions.&lt;br&gt;
&amp;gt; If n is divisible by 3,5,or 7, y is square root of n, otherwise, y is zero.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I tested with if-statements and array relational method, but the latter did not agree with the correct result (the if-statements is right in this case).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; n = 1:30;&lt;br&gt;
&amp;gt;    y= (n.^(1/2)).*(mod(n,3)==0) + (n.^(1/2)).*(mod(n,5)==0) + (n.^(1/2)).*(mod(n,7)==0) + 0;&lt;br&gt;
&amp;gt; sum(y)   % gives 79.3984&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; for n = 1:30&lt;br&gt;
&amp;gt;     if(mod(n,3) == 0)&lt;br&gt;
&amp;gt;         y(n) = n^(1/2);&lt;br&gt;
&amp;gt;     else if (mod(n,5)==0)&lt;br&gt;
&amp;gt;         y(n) = n^(1/2);&lt;br&gt;
&amp;gt;     else if(mod(n,7)==0)&lt;br&gt;
&amp;gt;         y(n) = n^(1/2);&lt;br&gt;
&amp;gt;     else&lt;br&gt;
&amp;gt;         y(n)= 0;&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt;     end&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt; sum(y)    % gives 65.4656&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I got the right answer by writing this little demo code&lt;br&gt;
&amp;gt; % z = [ 3 5 6 7 9 10 12 14 15 18 20 21 24 25 27 28 30]&lt;br&gt;
&amp;gt; % for n = 1:17&lt;br&gt;
&amp;gt; %     g(n) = z(n).^(1/2);&lt;br&gt;
&amp;gt; % end&lt;br&gt;
&amp;gt; % sum(g)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My question is, where is my mistake??? Can anyone spot one? &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thank you!&lt;br&gt;
- - - - - - - - -&lt;br&gt;
&amp;nbsp;&amp;nbsp;In your first method the integers 15, 21, and 30 each get counted twice because they are each multiples of two of the divisors 3, 5, and 7, not just one.  That accounts for the difference in your results:&lt;br&gt;
&lt;br&gt;
&amp;nbsp;79.3984 - 65.4656 = sqrt(15) + sqrt(21) + sqrt(30)&lt;br&gt;
&lt;br&gt;
Roger Stafford</description>
    </item>
    <item>
      <pubDate>Thu, 03 Mar 2011 08:24:04 +0000</pubDate>
      <title>Re: simple array relational problem</title>
      <link>http://www.mathworks.nl/matlabcentral/newsreader/view_thread/303796#822825</link>
      <author>Jos (10584) </author>
      <description>"John Wong" wrote in message &amp;lt;iknd4l$her$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Given an array n is all integers from 1 to 30, and array y is given by two conditions.&lt;br&gt;
&amp;gt; If n is divisible by 3,5,or 7, y is square root of n, otherwise, y is zero.&lt;br&gt;
&lt;br&gt;
Reading only this, I think you might be interested in learning about BSXFUN and the power of logical indexing:&lt;br&gt;
&lt;br&gt;
n = 1:30&lt;br&gt;
d = [3 5 7] &lt;br&gt;
&lt;br&gt;
% these 2 lines can be combined&lt;br&gt;
tf1 = bsxfun(@(x,y) rem(x,y)==0, n, d.')&lt;br&gt;
tf2 = any(tf1,1) &lt;br&gt;
&lt;br&gt;
% filling in the values&lt;br&gt;
y = zeros(size(n)) % pre-allocation&lt;br&gt;
y(tf2) = n(tf2)&lt;br&gt;
&lt;br&gt;
~ Jos</description>
    </item>
  </channel>
</rss>
