简体   繁体   English

MATLAB min(array)给出的索引超出了数组的维数

[英]MATLAB min(array) gives index exceeds array dimensions

I am trying to find the minimum value of a function of two variables, and then find the value of the variables. 我试图找到两个变量的函数的最小值,然后找到变量的值。

My method is to iterate the function through several values of the variables and then use the min function to find the lowest value. 我的方法是遍历变量的多个值,然后使用min函数找到最小值。

minval = -10;
maxval = 10;
n = 1;

for x = minval:maxval
    for y = minval:maxval
        f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
        n=n+1;
    end
end
f(n) = abs(x-1)+abs(y-1)+abs(x-3)+abs(y-5)+abs(x-8)+abs(y-3);
fmin = min(f)

The problem is with the last line: 问题出在最后一行:

fmin = min(f)

I am getting the error 我收到错误

??? Index exceeds matrix dimensions.

Error in ==> Lab2 at 65
fmin = min(f)

Why is this? 为什么是这样? Any help is greatly appreciated. 任何帮助是极大的赞赏。

Don't define a variable named min. 不要定义一个名为min的变量。 Try this: 尝试这个:

which min

What does it tell you? 它告诉你什么?

Note that functions in MATLAB can be overloaded by creating variables with the same name. 请注意,可以通过创建具有相同名称的变量来重载MATLAB中的函数。 When you do so, you prevent the function from being accessed by MATLAB. 这样做时,可以防止MATLAB访问该函数。 This is RARELY a good idea, so don't do it. 这很少是个好主意,所以不要这样做。 The solution is 解决方案是

clear min

So you will delete that variable you have created. 因此,您将删除已创建的变量。 Of course, if there was something important in that variable, stuff it somewhere else first. 当然,如果该变量中有重要内容,请先将其填充到其他位置。

This code runs perfectly when I plug it into my version of Matlab. 当我将其插入我的Matlab版本中时,该代码可以完美运行。

If the error is occuring on line 65, then there must be something else going on in your program. 如果错误发生在第65行,则程序中肯定还有其他情况在发生。 Try and turn this part of your program into a function, so that it won't be impacted by everything else that you're working on. 尝试将程序的这一部分变成一个函数,以使它不会受到您正在处理的其他所有内容的影响。

It does indeed look like you have declared a variable called min and so Matlab now treats it like a variable and not a function so it thinks you are trying to index the variable min with the vector f. 确实确实看起来像您已经声明了一个名为min的变量,因此Matlab现在将其视为变量而不是函数,因此它认为您正在尝试使用向量f索引变量min。

But just a comment on your code, leaving off whatever f(442) is you could achieve the same thing in a much more matlabesque fashion without loops like this: 但是,仅对您的代码进行注释,不考虑f(442)的问题,您就可以以更复杂的方式实现相同的功能,而无需像这样的循环:

minval = -10;
maxval = 10;

X = minval:maxval;
Y = X;

[xx, yy] = meshgrid(X, Y);
F = abs(xx-1) + abs(yy-1) + abs(xx-3) + abs(yy-5) +abs(xx-8) + abs(yy-3);

Your f is now equivalent to F(:)' (without the final value...), prove it to yourself like this: sum(f(1:end-1) == F(:)') 您的f现在等于F(:)' (没有最终值...),像这样向自己证明: sum(f(1:end-1) == F(:)')

F as a matrix probably makes more sense than f as a flat vector anyway and you can find the min of F like this: min(F(:)) 无论如何,作为矩阵的F可能比作为平面向量的f更有意义,您可以这样找到F的最小值: min(F(:))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM