为什么会出现错误“索引超出矩阵尺寸"? [英] Why am I getting the error "Index exceeds matrix dimensions"?

查看:94
本文介绍了为什么会出现错误“索引超出矩阵尺寸"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前是MATLAB的新手.我的代码如下.我只是有一个问题,为什么我对于提供的功能会不断收到错误索引超出矩阵维数":

I am currently new to MATLAB. My code is below. I just have a question regarding why I keep getting the error "Index exceeds matrix dimensions" for the functions provided:

a = [105 97 245 163 207 134 218 199 160 196 221 154 228 131 180 178 157 151 ...
     175 201 183 153 174 154 190 76 101 142 149 200 186 174 199 115 193 167 ...
     171 163 87 176 121 120 181 160 194 184 165 145 160 150 181 168 158 208 ...
     133 135 172 171 237 170 180 167 176 158 156 229 158 148 150 118 143 141 ...
     110 133 123 146 169 158 135 149];

mean = mean(a)
std = std(a)
max = max(a)
min = min(a)
range = range(a)

推荐答案

不要给变量提供与现有函数相同的名称.此遮盖了该功能.然后,当您尝试使用参数调用函数时,您最终会索引带有自变量的变量,在这种情况下,该变量将尝试为变量中不存在的元素建立索引,从而导致您的错误.

Don't give variables the same names as existing functions. This shadows the function. When you then try to call the function with an argument you instead end up indexing the variable with the argument, which in this case tries to index elements in the variable that don't exist, hence your error.

使用 clear 删除现有变量,然后用新的变量名重新运行计算:

Use clear to remove the existing variables, then rerun the calculations with new variable names:

clear mean std max min range;
meanResult = mean(a);
stdResult = std(a);
...

这篇关于为什么会出现错误“索引超出矩阵尺寸"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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