Matlab beginner, making variables

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
2 replies · 3K views
Dell
Messages
555
Reaction score
0
in MATLAB how do i make a variable which is dependent on another variable, for example

y=2*a+3 for any a,
so that when i change a, y will automatically change

i have only managed to set a value for a, then set y and i get a fixed value for y
 
Physics news on Phys.org
Dell said:
in MATLAB how do i make a variable which is dependent on another variable, for example

y=2*a+3 for any a,
so that when i change a, y will automatically change

i have only managed to set a value for a, then set y and i get a fixed value for y

Right, and you can place whatever value or values in 'a' that you like. For instance, if 'a' is a vector containing 7 different values, and the formula is written correctly, then 'y' will contain 7 values, each that function of the corresponding element in 'a':

>> a = [-1 0 1 2 3 4 5]

a =

-1 0 1 2 3 4 5

>> y = 2 * a + 3

y =

1 3 5 7 9 11 13
-Will Dwinnell
http://matlabdatamining.blogspot.com/"
 
Last edited by a moderator:
Sounds like you're looking for functions. If you're function is simple, you can use an anonymous function (no separate m-file required) or you can use a separate m-file function with a function handle.

Check out the Mathworks site and search for 'Anonymous Functions' or 'Function Handles' (or search within MATLAB's help).

Is this what you're looking for?

-Kerry