I am having trouble with the max and min functions. I am just starting Matlab

7 views (last 30 days)
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)

Answers (1)

Star Strider
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
Brian Johnson
Brian Johnson on 8 Mar 2019
I renamed them to different variable names and it still throws the error. Originally the code ran but then when I ran it a second time, it threw the error and hasn't changed since.
Star Strider
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!