下标索引必须是实数正整数或逻辑,通用解决方案 [英] Subscript indices must either be real positive integers or logicals, generic solution

查看:35
本文介绍了下标索引必须是实数正整数或逻辑,通用解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经常出现以下错误:

<块引用>

下标索引必须是正整数或逻辑数

我发现了很多关于此的问题,但没有一个具有真正通用的答案.因此,我想拥有处理此问题的通用解决方案.

解决方案

下标索引必须是正整数或逻辑数

在几乎所有情况下,此错误都是由两个原因之一引起的.幸运的是,对此有一个简单的检查.

首先确保您位于发生错误的行,这通常可以通过在运行函数或脚本之前使用 dbstop if error 来实现.现在我们可以检查第一个问题:

1.某处无效索引用于访问变量

找到每个变量,看看它们是如何被索引的.被索引的变量通常采用以下形式之一:

variableName(index,index)变量名{索引,索引}变量名{索引}(索引)

现在只需查看括号之间的内容,然后选择每个索引.然后点击 f9 来评估结果并检查它是一个实数的正整数还是逻辑的.目视检查通常就足够了(请记住,可接受的值为 true、false 或 1,2,3,... BUT NOT 0),但对于大型矩阵,您可以使用诸如 之类的东西isequal(index, round(index)),或更确切地说 isequal(x, max(1,round(abs(x)))) 来检查实数正整数.要检查类,您可以使用 class(index) 如果值都是 'true' 或 'false',它应该返回 'logical'.

确保检查评估每个索引,即使是那些看起来不寻常的索引,如下例所示.如果所有索引都检查出来,您可能面临第二个问题:

2.函数名被用户定义的变量掩盖了

MATLAB 函数通常有非常直观的名称.这很方便,但有时会导致意外重载(内置)函数,即创建一个与函数同名的变量,例如您可以使用 max = 9 ,其余的脚本/函数Matlab 会认为 max 是一个变量而不是函数 max 所以如果你尝试类似 max([1 8 0 3 7]) 因为 Matlab 现在假设您尝试索引变量 max0 是无效索引,而不是返回该向量的最大值.>

为了检查您拥有哪些变量,您可以查看工作区.但是,如果您正在寻找一种系统的方法,这里是一个:

对于每一个后面跟有括号()并且在步骤1中没有被确认有正确索引的字母或单词.检查它是否实际上是一个变量.这可以通过使用 which 轻松完成.

<小时>

示例

简单出现无效索引

a = 1;b = 2;c = 3;a(b/c)

这里我们将评估 b/c 并发现它不是一个很好的四舍五入数字.

无效索引的复杂发生

a = 1;b = 2;c = 3;d = 1:10;a(b+mean(d(cell2mat({b}):c)))

我建议由内而外工作.所以首先评估被索引的最内部变量:d.结果证明 cell2mat({b}):c 可以很好地计算为整数.然后评估 b+mean(d(cell2mat({b}):c)) 并发现我们没有整数或逻辑作为 a 的索引.

这里我们将评估 b/c 并发现它不是一个很好的四舍五入数字.

重载函数

表示% 一些目录文件名.m

你应该看到类似这样的东西来确认某物是一个函数.

a = 1:4;b=0:0.1:1;平均值(一)= 2.5;平均值(b);

在这里我们看到 mean 被意外分配给了.现在我们得到:

表示%均值是一个变量.

The following error occurs quite frequently:

Subscript indices must either be real positive integers or logicals

I have found many questions about this but not one with a really generic answer. Hence I would like to have the general solution for dealing with this problem.

解决方案

Subscript indices must either be real positive integers or logicals

In nearly all cases this error is caused by one of two reasons. Fortunately there is an easy check for this.

First of all make sure you are at the line where the error occurs, this can usually be achieved by using dbstop if error before you run your function or script. Now we can check for the first problem:

1. Somewhere an invalid index is used to access a variable

Find every variable, and see how they are being indexed. A variable being indexed is typically in one of these forms:

variableName(index,index)
variableName{index,index}
variableName{indices}(indices)

Now simply look at the stuff between the brackets, and select every index. Then hit f9 to evaluate the result and check whether it is a real positive integer or logical. Visual inspection is usually sufficient (remember that acceptable values are in true,false or 1,2,3,... BUT NOT 0) , but for a large matrix you can use things like isequal(index, round(index)), or more exactly isequal(x, max(1,round(abs(x)))) to check for real positive integers. To check the class you can use class(index) which should return 'logical' if the values are all 'true' or 'false'.

Make sure to check evaluate every index, even those that look unusual as per the example below. If all indices check out, you are probably facing the second problem:

2. A function name has been overshadowed by a user defined variable

MATLAB functions often have very intuitive names. This is convenient, but sometimes results in accidentally overloading (builtin) functions, i.e. creating a variable with the same name as a function for example you could go max = 9 and for the rest of you script/function Matlab will consider max to be a variable instead of the function max so you will get this error if you try something like max([1 8 0 3 7]) because instead of return the maximum value of that vector, Matlab now assumes you are trying to index the variable max and 0 is an invalid index.

In order to check which variables you have you can look at the workspace. However if you are looking for a systematic approach here is one:

For every letter or word that is followed by brackets () and has not been confirmed to have proper indices in step 1. Check whether it is actually a variable. This can easily be done by using which.


Examples

Simple occurrence of invalid index

a = 1;
b = 2;
c = 3;
a(b/c)

Here we will evaluate b/c and find that it is not a nicely rounded number.

Complicated occurrence of invalid index

a = 1;
b = 2;
c = 3;
d = 1:10;
a(b+mean(d(cell2mat({b}):c)))

I recommend working inside out. So first evaluate the most inner variable being indexed: d. It turns out that cell2mat({b}):c, nicely evaluates to integers. Then evaluate b+mean(d(cell2mat({b}):c)) and find that we don't have an integer or logical as index to a.

Here we will evaluate b/c and find that it is not a nicely rounded number.

Overloaded a function

which mean 
% some directoryfilename.m

You should see something like this to actually confirm that something is a function.

a = 1:4;
b=0:0.1:1;
mean(a) = 2.5;
mean(b);

Here we see that mean has accidentally been assigned to. Now we get:

which mean
% mean is a variable.

这篇关于下标索引必须是实数正整数或逻辑,通用解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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