简体   繁体   English

我如何解决这个 MATLAB“矩阵维度必须一致”错误?

[英]How can I solve this MATLAB "Matrix dimensions must agree" error?

I am typing some code for a class but every time I run the function I get the same error:我正在为一个类键入一些代码,但每次运行该函数时,我都会遇到相同的错误:

??? Error using ==> plus
Matrix dimensions must agree.

Error in ==> Test at 6
f32=3.*exp((-x2.^2-y1.^2)./3);

I know that the problem is a simple index error, but I can't seem to find it anywhere.我知道问题是一个简单的索引错误,但我似乎无法在任何地方找到它。 Can somebody help me?有人可以帮助我吗?

Also I'm having the same problem with the following line of code:此外,我在以下代码行中遇到了同样的问题:

f34=(exp(-0.3./x2))./(log(y2).*sqrt(x2));

EDIT #1:编辑#1:

x2 is defined as 0:0.1:5 and y1 is defined as -5:0.1:5 , but that is what I have been assigned to define them as. x2被定义为0:0.1:5并且y1被定义为-5:0.1:5 ,但这就是我被分配来定义它们的原因。 And I know exp is not a function because I have used it elsewhere in my file.而且我知道 exp 不是一个函数,因为我在文件的其他地方使用过它。

EDIT #2:编辑#2:

OK.好的。 So if I can't use my current x and y is there anyway I can define them to keep them on those bounds while still making them the same size?因此,如果我不能使用我当前的 x 和 y 无论如何我可以定义它们以将它们保持在这些边界上,同时仍然使它们具有相同的大小?

UPDATE:更新:

OK, now that you have confirmed that your variables x2 and y1 contain different numbers of elements, you have a couple of solutions to choose from:好的,现在您已经确认变量x2y1包含不同数量的元素,您有几个解决方案可供选择:

  1. For each variable, you can create a set number of values over the respective ranges using the function LINSPACE .对于每个变量,您可以使用函数LINSPACE在各自的范围内创建一定数量的值。 For example:例如:

     x2 = linspace(0,5,101); %# 101 values spanning the range 0 to 5 y1 = linspace(-5,5,101); %# 101 values spanning the range -5 to 5

    However, when you compute the result f32 (which will also be a 101-element array), it will only be evaluated at the respective pairs of values in x2 and y1 (eg x2(1) and y1(1) , x2(50) and y1(50) , etc.).但是,当您计算结果f32 (也将是一个 101 个元素的数组)时,它只会在x2y1中的相应值对(例如x2(1)y1(1)x2(50)y1(50)等)。

  2. If you would rather evaluate f32 at every unique pair of points over the ranges of x2 and y1 , you should instead use the function MESHGRID to generate your values.如果您更愿意在x2y1范围内的对唯一点对f32进行评估,您应该使用函数MESHGRID来生成您的值。 This will also allow you to have a different numbers of points over the ranges for x2 and y1 :这也将允许您在x2y1的范围内拥有不同数量的点:

     [x2,y1] = meshgrid(0:0.1:5,-5:0.1:5);

    The above will create x2 and y1 as 101-by-51 arrays such that f32 will also be a 101-by-51 array evaluated at all the points over the given ranges of values.上面将创建x2y1作为 101×51 数组,这样f32也将是一个 101×51 数组,在给定值范围内的所有点处进行评估。

Previous answer:上一个答案:

The first thing to test is if all the variables you are putting into the equation are the same size or scalar values, which they would have to be since you are using element-wise operators like .^ and .* .首先要测试的是,您放入方程的所有变量是否都是相同的大小或标量值,因为您使用的是元素运算符,如.^.* ,所以它们必须是相同的。 For the first equation, see what output you get when you do this:对于第一个等式,请查看执行此操作时得到的输出:

size(x2)
size(y1)

If they give the same result, or either is [1 1] , then that's not your problem.如果他们给出相同的结果,或者是[1 1] ,那么这不是你的问题。

The next thing to check is whether or not you have shadowed the EXP function by creating a variable by the name exp .接下来要检查的是您是否通过创建名为exp的变量来隐藏EXP函数。 If you're running the code as a script in the command window, type whos and see if a variable named exp shows up.如果您在命令窗口中将代码作为脚本运行,请键入whos并查看是否显示名为exp的变量。 If it does, you need to delete or rename it so that you can use the function EXP.如果是,您需要删除或重命名它,以便您可以使用函数EXP。

How do you expect -x2.^2-y1.^2 to work when x2 and y1 is of different size?x2y1大小不同时,您如何期望-x2.^2-y1.^2工作? x2=0:0.1:5 has fifty or so entires while y1=-5:0.1:5 has a hundred or so entries. x2=0:0.1:5有五十个左右的条目,而y1=-5:0.1:5有一百个左右的条目。

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

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