如何解决这个MATLAB“矩阵尺寸必须一致"的问题?错误? [英] How can I solve this MATLAB "Matrix dimensions must agree" error?

查看:198
本文介绍了如何解决这个MATLAB“矩阵尺寸必须一致"的问题?错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为某个类输入一些代码,但是每次运行该函数时,都会遇到相同的错误:

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));

编辑#1:

x2被定义为0:0.1:5,而y1被定义为-5:0.1:5,但这就是我所指定的定义.而且我知道exp不是函数,因为我在文件的其他地方使用了它.

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. And I know exp is not a function because I have used it elsewhere in my file.

编辑#2:

好.因此,如果我不能使用当前的x和y,我可以定义它们以使它们保持在这些边界上,同时仍使它们具有相同的大小吗?

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?

推荐答案

更新:

好的,现在您已经确认变量x2y1包含不同数量的元素,您可以选择以下两种解决方案:

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:

  1. 对于每个变量,您可以使用函数 LINSPACE .例如:

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

但是,当您计算结果f32(也将是101个元素的数组)时,将仅以x2y1中的相应值对(例如,x2(1)y1(1)x2(50)y1(50)等).

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 (e.g. x2(1) and y1(1), x2(50) and y1(50), etc.).

如果您希望在 x2y1范围内的每个唯一对点上评估f32,则应该使用函数

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. This will also allow you to have a different numbers of points over the ranges for x2 and y1:

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

上面的代码将创建x2y1作为101 x 51数组,这样f32还将是在给定值范围内的所有点处进行评估的101 x 51数组.

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.

上一个答案:

要测试的第一件事是,要放入方程式中的所有变量是否具有相同的大小或标量值,因为使用的是像.^.*这样的元素运算符,它们必须是相同的大小或标量值.对于第一个等式,请查看执行此操作时得到的输出:

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)

如果他们给出相同的结果,或者是[1 1],那么这不是您的问题.

If they give the same result, or either is [1 1], then that's not your problem.

接下来要检查的是您是否已经遮盖了 EXP exp的变量来创建a>函数.如果您在命令窗口中以脚本形式运行代码,请键入 whos ,然后查看是否显示了名为exp的变量.如果是这样,则需要删除或重命名它,以便可以使用功能 EXP.

The next thing to check is whether or not you have shadowed the EXP function by creating a variable by the name 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. If it does, you need to delete or rename it so that you can use the function EXP.

这篇关于如何解决这个MATLAB“矩阵尺寸必须一致"的问题?错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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