此错误消息是什么意思?下标索引必须是实数正整数或逻辑值 [英] What does it this error message mean? Subscript indices must either be real positive integers or logicals

查看:73
本文介绍了此错误消息是什么意思?下标索引必须是实数正整数或逻辑值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调试样本的简单统计信息.错误消息:

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

推荐答案

之所以会出现此错误,是因为您在编写mean = mean(s)时覆盖了内置变量.其他所有功能也是如此.

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.

如果以此方式进行操作,则第一次调用脚本时,它将计算均值.但是,MATLAB第二次将mean(s)解释为变量mean的第s个值.显然不可能获得标量的第0.9个元素,因此会收到错误消息.

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.

应该要做的是:

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

也就是说,给您的变量命名不要与内置函数混淆.

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

为了清楚起见,当您尝试执行此操作时,仍然会遇到相同的错误.确保clear工作空间,或者至少清除名称与内置函数名称冲突的变量.

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.

这篇关于此错误消息是什么意思?下标索引必须是实数正整数或逻辑值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆