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

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

问题描述

以下错误很常见:


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

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.

首先确保你处于发生错误的行,这通常可以通过使用 dbstop if error ,然后再运行你的函数或脚本。现在我们可以检查第一个问题:

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:

查找每个变量,并查看它们是如何被索引的。索引的变量通常是以下形式之一:

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)

现在简单看一下括号,并选择每个索引。然后点击 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。

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:

MATLAB函数通常有非常直观的名称。这很方便,但有时会导致意外重载(内置)功能,即创建与函数名称相同的变量,例如您可以去 max = 9 其余的脚本/函数Matlab将考虑 max 作为变量,而不是函数 max ,所以你会得到这个如果您尝试像 max([1 8 0 3 7])的错误,因为不是返回该向量的最大值,Matlab现在假设您正在尝试索引变量 max 0 是无效的索引。

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:

对于括号()并且没有被确认在步骤1中具有适当的索引。检查它是否实际上是一个变量。这可以通过使用轻松完成。

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.

无效索引的简单出现

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

这里我们将评估 b /

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

无效索引的复杂发生

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)),发现我们没有一个整数或逻辑作为索引到$ c> a 。

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.

这里我们将评估 b / c ,发现它不是一个很好的数字。

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

重载功能

which mean 
% some directory\filename.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天全站免登陆