Why am I getting the error “Index exceeds matrix dimensions”?

隐身守侯 提交于 2020-01-09 11:43:00

问题


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)

回答1:


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.

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);
...



回答2:


Use clc (clear command window), clear (removes all variables from the workspace) and close all (closes off any previously used figures) to clean you work space. This could help run the script better.

clc, clear, close all

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)


来源:https://stackoverflow.com/questions/47802940/why-am-i-getting-the-error-index-exceeds-matrix-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!