I am having trouble with the max and min functions. I am just starting Matlab
7 views (last 30 days)
Show older comments
Whenver I run my code, either the max or min works while the other throws an error saying "Index exceeds array bounds" While other times, neither of them work. I am not sure how to fix it. Please help
weight = 4;
N = 2;
range = [weight-N;weight+N];
max = max(range)
min = min(range)
1 Comment
madhan ravi
on 8 Mar 2019
clear all
clear global
weight = 4;
N = 2;
range = [weight-N;weight+N];
max = max(range)
min = min(range)
Answers (1)
Star Strider
on 8 Mar 2019
Edited: Star Strider
on 8 Mar 2019
Most likely, you have named a variable either ‘max’ or ‘min’ somewhere in your workspace. That’s likely the problem, since the code you posted runs for me without error.
Run these from your Command Window or a script:
which('max', '-all')
which('min', '-all')
if the first result from either is:
max is a variable.
or:
min is a variable.
you have found the problem.
The solution is to rename your variables so their names do not conflict with MATLAB functions.
This is called ‘overshadowing’ of MATLAB functions, and is to be absolutely avoided, for obvious reasons.
2 Comments
Star Strider
on 8 Mar 2019
I still believe you are overshadowing those functions.
You must have other variables named ‘max’ and ‘min’ that still exist in your workspace.
Run the which calls again. Keep looking until you find the other instances of those variables.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!