下标错误:sum = sum(s) [英] Subscript error: sum = sum(s)

查看:33
本文介绍了下标错误:sum = sum(s)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的代码,其中 hY47x1 向量.

Below is my code, where h and Y are 47x1 vectors.

s = h-Y;
sum = sum(s);

我收到此错误:

error: sum(6057.48): 下标必须是整数 1 到 (2^31)-1 或逻辑数

error: sum(6057.48): subscripts must be either integers 1 to (2^31)-1 or logicals

有人能解释一下为什么吗?

Can somebody please explain why?

推荐答案

正如在 Sardar Usama 的评论中提到的,你不能使用变量名 sum想使用内置函数sum.

As mentioned in the comments by Sardar Usama, you cannot use the variable name sum when you also want to use the built-in function sum.

默认情况下,sum 是一个函数,按预期在脚本中使用.

By default, sum is a function, used as you expected it to work in your script.

例如,在 Octave 中,您可以掩盖内置函数

In Octave, you are permitted to overshadow a built-in function, for instance

sum = 4; % Now there is a workspace variable, sum, with the value 4

执行此操作时,关键字sum 现在代表变量,而不是函数.强烈建议不要覆盖默认函数,即使您不打算在该脚本中使用它们!

When you do this, the keyword sum now stands for the variable, not the function. It's strongly advisable not to overwrite default functions, even if you're not planning on using them in that script!

要消除阴影,只需清除变量,

To remove your overshadowing, just clear the variable,

clear sum % Now it should behave as expected.

在继续之前必须清除变量的原因是与您的工作区有关.即使在程序退出后,变量 sum 仍保留在您的工作区中,因此当您再次运行它时(即使您选择了一个新的变量名称),您仍然已经更改了 sum被解释.通过清除该变量,Octave 会将其从您的工作区中删除后恢复为默认行为.

The reason you must clear the variable before continuing is to do with your workspace. The variable sum remains in your workspace even after the program exited, so when you run it again (even if you've chosen a new variable name) you have still already changed how sum is interpreted. By clearing the variable, Octave sets it back to its default behaviour as it is removed from your workspace.

总结:使用不同的变量名.

Summary: use a different variable name.

s = h-Y;
mysum = sum(s); % mysum isn't a built-in, so no clashes here!

这篇关于下标错误:sum = sum(s)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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