How To Assign Operator/Variables with a value

10 visualizaciones (últimos 30 días)
Yanuar Rizki Pahlevi
Yanuar Rizki Pahlevi el 6 de Oct. de 2020
Respondida: Walter Roberson el 7 de Oct. de 2020
Hi, I am currently working on a task of Linear Genetic Programming,
I have a question, how is the way to create the variable register and operator register in matlab?
I need to create that
1= +, 2= -, 3= *,4=/ and other operator
and variable register
1=x1, and so on.
I use ' ' and put them in matrix, but I cannot use them to calculation while I am evaluating the chromosome.
lets say I have A=[1 2 3 4], this means
X=2*4
(1 is X, and 3=*)
can anyone help me?

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Oct. de 2020
functions = {@plus, @minus, @times, @rdivide};
Now you can take the character representing the operator, subtract '0' (the character for the digit 0), and use that to index functions to get the function handle of the operator to execute.
If you need different variables, then create a vector of zeros, and take the character representing the variable number, subtract '0' and use that to index the variables vector.

Más respuestas (1)

Peter O
Peter O el 6 de Oct. de 2020
I'm not familiar with LGP.
Consider a cell array to hold the chromosome?
C = {X, 2, @times, 4}
Using the operator definitions with function handles permits you to assign multiple and unknown operators. Alternatively, you can code them like you had 1=+, 2=-, etc and use an if/else on eval of the chromosome.
If permitted, I'd also suggest considering RPN notation to handle the arguments. A little cleaner/easier ordering.
C = {X, @times, 2, 4} --> Assign X @times(2,4)
Handles for 4 Fcn Basics: @plus, @minus, @times, @mrdivide
  1 comentario
Yanuar Rizki Pahlevi
Yanuar Rizki Pahlevi el 7 de Oct. de 2020
I tried put 1=+, 2=-, and so on. but it did not work
is it possible to use 1=@plus?

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by