help with editing variables

Hello, a=2; b=a+3 How to modify the constant "a" after I declare this constant, to modify the variable b?

Respuestas (2)

Andrei Bobrov
Andrei Bobrov el 29 de Nov. de 2011

0 votos

f1 = @(a)a + 3
b = f1(a)
Daniel Shub
Daniel Shub el 29 de Nov. de 2011

0 votos

While there are ways to do this (e.g., functions, function handles and handle classes), this is not really how MATLAB works. In MATLAB expressions become variables, which are constants, as soon as you evaluate the expression (i.e., press enter or run you script). This means that
clear x y
z = x+y;
gives an error since x and y are not defined (I cleared them).
x = 1;
y = 2;
z = x+y;
z
x = 10;
z
returns 3 in both cases since z loses its dependence on x and y as soon as it is evaluated.

La pregunta está cerrada.

Etiquetas

Preguntada:

el 29 de Nov. de 2011

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by