Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab beginner, making variables

  1. Mar 13, 2009 #1
    in matlab how do i make a variable which is dependant 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
  2. jcsd
  3. Mar 13, 2009 #2
    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/" [Broken]
    Last edited by a moderator: May 4, 2017
  4. Mar 13, 2009 #3
    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?

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook