<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924</link>
    <title>MATLAB Central Newsreader - Random for basic artihmetic operations</title>
    <description>Feed for thread: Random for basic artihmetic operations</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>Thu, 21 May 2009 21:43:22 -0400</pubDate>
      <title>Random for basic artihmetic operations</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924#651545</link>
      <author>Sven Schulz</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
how can i design a random choice for basic arithmetic operations?&lt;br&gt;
&lt;br&gt;
Example:&lt;br&gt;
&lt;br&gt;
First random choice  A+B*C/E&lt;br&gt;
Second random choice A*B-C+E&lt;br&gt;
Third random choice  A-B+C+E&lt;br&gt;
an so on . . .&lt;br&gt;
&lt;br&gt;
I have no idea to implement M-Code to solve this problem.&lt;br&gt;
&lt;br&gt;
Regards&lt;br&gt;
&lt;br&gt;
Sven</description>
    </item>
    <item>
      <pubDate>Fri, 22 May 2009 01:57:01 -0400</pubDate>
      <title>Re: Random for basic artihmetic operations</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924#651567</link>
      <author>Justin Abbott</author>
      <description>&quot;Sven Schulz&quot; &amp;lt;sven-schu@arcor.de&amp;gt; wrote in message &amp;lt;4a15cb07$0$31332$9b4e6d93@newsspool4.arcor-online.net&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; how can i design a random choice for basic arithmetic operations?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Example:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; First random choice  A+B*C/E&lt;br&gt;
&amp;gt; Second random choice A*B-C+E&lt;br&gt;
&amp;gt; Third random choice  A-B+C+E&lt;br&gt;
&amp;gt; an so on . . .&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have no idea to implement M-Code to solve this problem.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Regards&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sven&lt;br&gt;
&lt;br&gt;
I am not really sure exactly what you want, but if you stick with the four possible operations you have in your example and assume that A,B,C,D are scalars&lt;br&gt;
you could try something along the lines of:&lt;br&gt;
&lt;br&gt;
% Cell array of function handles to the desired operations&lt;br&gt;
op = {@plus @times @minus @rdivide};   &lt;br&gt;
% Pick 3 operations with replacement&lt;br&gt;
opDraw = ceil(4*rand(3,1));&lt;br&gt;
val = op{opDraw(1)}(A,op{opDraw(2)}(op{B,opDraw(3)}(C,D)));&lt;br&gt;
&lt;br&gt;
After writing this down it is really ugly, so you might want to do it recursively.  Additionally it ignores any sort of operation precedent rules, but it is some sort of start.&lt;br&gt;
&lt;br&gt;
-Justin</description>
    </item>
    <item>
      <pubDate>Mon, 25 May 2009 21:01:41 -0400</pubDate>
      <title>Re: Random for basic artihmetic operations</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924#652314</link>
      <author>Sven Schulz</author>
      <description>Justin Abbott wrote:&lt;br&gt;
&amp;gt; % Cell array of function handles to the desired operations&lt;br&gt;
&amp;gt; op = {@plus @times @minus @rdivide};&lt;br&gt;
&amp;gt; % Pick 3 operations with replacement&lt;br&gt;
&amp;gt; opDraw = ceil(4*rand(3,1));&lt;br&gt;
&amp;gt; val = op{opDraw(1)}(A,op{opDraw(2)}(op{B,opDraw(3)}(C,D)));&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;nbsp;Hi Justin,&lt;br&gt;
thank you for your inspiration.&lt;br&gt;
&lt;br&gt;
My favorite code could luck like this&lt;br&gt;
&lt;br&gt;
A=1;&lt;br&gt;
B=2;&lt;br&gt;
C=3;&lt;br&gt;
&lt;br&gt;
arth_operations = [+ - * /];&lt;br&gt;
&lt;br&gt;
Result = Arandom(arth_operations)Brandom(arth_operations)C&lt;br&gt;
&lt;br&gt;
But, no idea to create code like this.&lt;br&gt;
&lt;br&gt;
Sven</description>
    </item>
    <item>
      <pubDate>Mon, 25 May 2009 22:07:01 -0400</pubDate>
      <title>Re: Random for basic artihmetic operations</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924#652318</link>
      <author>Sadik </author>
      <description>How about this one?&lt;br&gt;
&lt;br&gt;
A = 1;&lt;br&gt;
B = 2;&lt;br&gt;
C = 3;&lt;br&gt;
&lt;br&gt;
arth_operations = ['+','-','*','/'];&lt;br&gt;
&lt;br&gt;
opDraw = ceil(4*rand(2,1));&lt;br&gt;
opDraw(opDraw==0)=1; %If there are any zeroes, they should be 1.&lt;br&gt;
&lt;br&gt;
eval(['Result = A' arth_operations(opDraw(1)) 'B' arth_operations(opDraw(2)) 'C;'])&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Sven Schulz&quot; &amp;lt;sven-schu@arcor.de&amp;gt; wrote in message &amp;lt;4a1b0740$0$30229$9b4e6d93@newsspool1.arcor-online.net&amp;gt;...&lt;br&gt;
&amp;gt; Justin Abbott wrote:&lt;br&gt;
&amp;gt; &amp;gt; % Cell array of function handles to the desired operations&lt;br&gt;
&amp;gt; &amp;gt; op = {@plus @times @minus @rdivide};&lt;br&gt;
&amp;gt; &amp;gt; % Pick 3 operations with replacement&lt;br&gt;
&amp;gt; &amp;gt; opDraw = ceil(4*rand(3,1));&lt;br&gt;
&amp;gt; &amp;gt; val = op{opDraw(1)}(A,op{opDraw(2)}(op{B,opDraw(3)}(C,D)));&lt;br&gt;
&amp;gt;  &lt;br&gt;
&amp;gt;  Hi Justin,&lt;br&gt;
&amp;gt; thank you for your inspiration.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My favorite code could luck like this&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; A=1;&lt;br&gt;
&amp;gt; B=2;&lt;br&gt;
&amp;gt; C=3;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; arth_operations = [+ - * /];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Result = Arandom(arth_operations)Brandom(arth_operations)C&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; But, no idea to create code like this.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sven</description>
    </item>
    <item>
      <pubDate>Tue, 26 May 2009 12:01:55 -0400</pubDate>
      <title>Re: Random for basic artihmetic operations</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924#652430</link>
      <author>Sven Schulz</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
thank you, it works.&lt;br&gt;
&lt;br&gt;
What is the meaning of this line?&lt;br&gt;
&lt;br&gt;
opDraw(opDraw==0)=1; %If there are any zeroes, they should be 1.&lt;br&gt;
&lt;br&gt;
Sven</description>
    </item>
    <item>
      <pubDate>Tue, 26 May 2009 12:25:03 -0400</pubDate>
      <title>Re: Random for basic artihmetic operations</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924#652435</link>
      <author>Sadik </author>
      <description>It means, we are looking for the indices of opDraw for which opDraw = 0 and then replacing those zeroes with 1s because we cannot use a zero for indexing. &lt;br&gt;
&lt;br&gt;
For example, if you have a vector x = [1 2 3 4 5 3 4]; and you would like to change 3s into 7s, then you say:&lt;br&gt;
&lt;br&gt;
x(x==3)=7;&lt;br&gt;
&lt;br&gt;
so that you now have x = [1 2 7 4 5 7 4].&lt;br&gt;
&lt;br&gt;
Hope this helps.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Sven Schulz&quot; &amp;lt;sven-schu@arcor.de&amp;gt; wrote in message &amp;lt;4a1bda3a$0$31337$9b4e6d93@newsspool4.arcor-online.net&amp;gt;...&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; thank you, it works.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What is the meaning of this line?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; opDraw(opDraw==0)=1; %If there are any zeroes, they should be 1.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Sven</description>
    </item>
    <item>
      <pubDate>Thu, 28 May 2009 21:06:04 -0400</pubDate>
      <title>Re: Random for basic artihmetic operations</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/251924#653237</link>
      <author>Sven Schulz</author>
      <description>Sadik  wrote:&lt;br&gt;
&amp;gt; It means, we are looking for the indices of opDraw for which opDraw =&lt;br&gt;
&amp;gt; 0 and then replacing those zeroes with 1s because we cannot use a&lt;br&gt;
&amp;gt; zero for indexing.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; For example, if you have a vector x = [1 2 3 4 5 3 4]; and you would&lt;br&gt;
&amp;gt; like to change 3s into 7s, then you say:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; x(x==3)=7;&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; so that you now have x = [1 2 7 4 5 7 4].&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Hope this helps.&lt;br&gt;
&lt;br&gt;
Yes, i understand.&lt;br&gt;
&lt;br&gt;
This is a verry cool procedure to replace a number in a vector. I would &lt;br&gt;
use a for-next-loop with if-else . . .&lt;br&gt;
&lt;br&gt;
Sven </description>
    </item>
  </channel>
</rss>

