Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Random for basic artihmetic operations

Subject: Random for basic artihmetic operations

From: Sven Schulz

Date: 21 May, 2009 21:43:22

Message: 1 of 7

Hi,

how can i design a random choice for basic arithmetic operations?

Example:

First random choice A+B*C/E
Second random choice A*B-C+E
Third random choice A-B+C+E
an so on . . .

I have no idea to implement M-Code to solve this problem.

Regards

Sven

Subject: Random for basic artihmetic operations

From: Justin Abbott

Date: 22 May, 2009 01:57:01

Message: 2 of 7

"Sven Schulz" <sven-schu@arcor.de> wrote in message <4a15cb07$0$31332$9b4e6d93@newsspool4.arcor-online.net>...
> Hi,
>
> how can i design a random choice for basic arithmetic operations?
>
> Example:
>
> First random choice A+B*C/E
> Second random choice A*B-C+E
> Third random choice A-B+C+E
> an so on . . .
>
> I have no idea to implement M-Code to solve this problem.
>
> Regards
>
> Sven

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
you could try something along the lines of:

% Cell array of function handles to the desired operations
op = {@plus @times @minus @rdivide};
% Pick 3 operations with replacement
opDraw = ceil(4*rand(3,1));
val = op{opDraw(1)}(A,op{opDraw(2)}(op{B,opDraw(3)}(C,D)));

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.

-Justin

Subject: Random for basic artihmetic operations

From: Sven Schulz

Date: 25 May, 2009 21:01:41

Message: 3 of 7

Justin Abbott wrote:
> % Cell array of function handles to the desired operations
> op = {@plus @times @minus @rdivide};
> % Pick 3 operations with replacement
> opDraw = ceil(4*rand(3,1));
> val = op{opDraw(1)}(A,op{opDraw(2)}(op{B,opDraw(3)}(C,D)));
 
 Hi Justin,
thank you for your inspiration.

My favorite code could luck like this

A=1;
B=2;
C=3;

arth_operations = [+ - * /];

Result = Arandom(arth_operations)Brandom(arth_operations)C

But, no idea to create code like this.

Sven

Subject: Random for basic artihmetic operations

From: Sadik

Date: 25 May, 2009 22:07:01

Message: 4 of 7

How about this one?

A = 1;
B = 2;
C = 3;

arth_operations = ['+','-','*','/'];

opDraw = ceil(4*rand(2,1));
opDraw(opDraw==0)=1; %If there are any zeroes, they should be 1.

eval(['Result = A' arth_operations(opDraw(1)) 'B' arth_operations(opDraw(2)) 'C;'])


"Sven Schulz" <sven-schu@arcor.de> wrote in message <4a1b0740$0$30229$9b4e6d93@newsspool1.arcor-online.net>...
> Justin Abbott wrote:
> > % Cell array of function handles to the desired operations
> > op = {@plus @times @minus @rdivide};
> > % Pick 3 operations with replacement
> > opDraw = ceil(4*rand(3,1));
> > val = op{opDraw(1)}(A,op{opDraw(2)}(op{B,opDraw(3)}(C,D)));
>
> Hi Justin,
> thank you for your inspiration.
>
> My favorite code could luck like this
>
> A=1;
> B=2;
> C=3;
>
> arth_operations = [+ - * /];
>
> Result = Arandom(arth_operations)Brandom(arth_operations)C
>
> But, no idea to create code like this.
>
> Sven

Subject: Random for basic artihmetic operations

From: Sven Schulz

Date: 26 May, 2009 12:01:55

Message: 5 of 7

Hi,

thank you, it works.

What is the meaning of this line?

opDraw(opDraw==0)=1; %If there are any zeroes, they should be 1.

Sven

Subject: Random for basic artihmetic operations

From: Sadik

Date: 26 May, 2009 12:25:03

Message: 6 of 7

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.

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:

x(x==3)=7;

so that you now have x = [1 2 7 4 5 7 4].

Hope this helps.


"Sven Schulz" <sven-schu@arcor.de> wrote in message <4a1bda3a$0$31337$9b4e6d93@newsspool4.arcor-online.net>...
> Hi,
>
> thank you, it works.
>
> What is the meaning of this line?
>
> opDraw(opDraw==0)=1; %If there are any zeroes, they should be 1.
>
> Sven

Subject: Random for basic artihmetic operations

From: Sven Schulz

Date: 28 May, 2009 21:06:04

Message: 7 of 7

Sadik wrote:
> 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.
>
> 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:
>
> x(x==3)=7;
>
> so that you now have x = [1 2 7 4 5 7 4].
>
> Hope this helps.

Yes, i understand.

This is a verry cool procedure to replace a number in a vector. I would
use a for-next-loop with if-else . . .

Sven

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us