0

I am trying to debug a simple statistics of a sample. Error message:

Subscript indices must either be real positive integers or logicals

I been getting this error lately for some homework. What does this mean?

clc
format short g
s=[0.90 1.32 1.96 1.85 2.29 1.42 1.35 1.47 1.74 1.82...
1.30 1.47 1.92 1.65 2.06 1.55 1.95 1.35 1.78 2.14...
1.63 1.66 1.05 1.71 1.27];
mean=mean(s)
median=median(s) 
mode=mode(s)
max=max(s); min=min(s); 
range=max-min ,std=std(s) ,var=var(s)
cvcd=std/mean*100
Math
  • 3,334
  • 4
  • 36
  • 51

2 Answers2

1

You get that error because you're overwriting the built-in variables when you write: mean = mean(s). Same goes for all the other functions.

If you do it this way, the first time you call the script, it will calculate the mean. However, the second time, MATLAB will interpret mean(s) as the s'th value of your variable mean. It's clearly impossible to get the 0.9'th element of a scalar, thus you get an error message.

What you should have done is:

mean_val = mean(s);       
median_val = median(s);

That is, give your variables names that can't be confused with the built-in functions.

And just to be clear, when you try this and still get the same error. Make sure to clear the workspace, or at least clear the variables that have conflicting names with the built-in function names.

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
1

Your problem is, that variable names cover the build in functions. The first time you run mean=mean(s) it is okay, but the second time mean is the double result, which is indexed using a double.

Do not use variable names, which are identical to build in functions!

Daniel
  • 36,610
  • 3
  • 36
  • 69