Matlab beginner, making variables

Click For Summary
SUMMARY

In MATLAB, to create a variable that automatically updates based on another variable, use vectorized operations. For example, defining a = [-1 0 1 2 3 4 5] and then calculating y = 2 * a + 3 will yield a corresponding vector for y with values [1 3 5 7 9 11 13]. Additionally, for more complex dependencies, utilize anonymous functions or function handles as outlined in MATLAB's documentation.

PREREQUISITES
  • Basic understanding of MATLAB syntax
  • Familiarity with vector operations in MATLAB
  • Knowledge of anonymous functions in MATLAB
  • Understanding of function handles in MATLAB
NEXT STEPS
  • Explore MATLAB's documentation on Anonymous Functions
  • Research Function Handles in MATLAB
  • Learn about Vectorization Techniques in MATLAB
  • Practice creating Dynamic Variables in MATLAB scripts
USEFUL FOR

Beginner MATLAB users, educators teaching programming concepts, and anyone looking to understand variable dependencies in MATLAB.

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
 

Similar threads

  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K