内部矩阵尺寸必须一致吗? [英] Inner matrix dimensions must agree?

查看:141
本文介绍了内部矩阵尺寸必须一致吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个matlab脚本,因此,这个问题似乎很基础,而且显而易见,但此刻我有些困惑.

this is my first matlab script, so this question may seem basic and blindingly obvious, but I am a little stuck at the moment.

我有一个两行的matlab脚本:

I have a matlab script of two lines:

x = linspace(0,4*pi,100);
y = exp(-x) * sin(x);

我要关闭MathWorks上的创建2-D线图"教程.我想在0到4pi的范围内绘制f(x)= e ^(-x)sin(x),但是我得到的内部矩阵尺寸必须在第二行上同意误差.我不确定发生了什么,因为我认为目前还没有创建任何矩阵.任何帮助,将不胜感激!我缺少一些简单的语法吗?谢谢!

I'm going off the Create 2-D Line Graph tutorial on Mathworks. I want to plot f(x) = e^(-x)sin(x) over the range 0 to 4pi, but I get an inner matrix dimensions must agree error on the second line. I'm not sure what's going on, because I don't think I'm creating any matrices at the moment. Any help would be appreciated! Is there something simple with syntax that I am missing? Thanks!

推荐答案

这是一个非常简单的错误,可以解决,我承认这是大多数MATLAB程序员首次面对MATLAB时都会遇到的常见错误.具体来说,当您执行以下操作时:

This is a very simple error to resolve, and I'll admit that it's a common error that most MATLAB programmers face when facing MATLAB for the first time. Specifically, when you do this line:

y = exp(-x) * sin(x);

此操作假定您将执行矩阵乘法.您实际要做的是一个逐个元素操作.您希望exp(-x)中的点与sin(x)中的相应元素相乘. @ellieadam提供了一些不错的链接供您查看这些操作是什么,但是如果要进行逐元素操作,则需要在之前添加(.) 乘法运算符.因此,您需要执行以下操作:

This operation is assuming that you will perform a matrix multiplication. What you actually want to do is an element-by-element operation. You want the points in exp(-x) to multiply with the corresponding elements in sin(x). @ellieadam provided some nice links for you to review what these operations are, but if you want to do element-by-element operations, you need to add a dot (.) before the multiplication operator. As such, you need to do this instead:

y = exp(-x) .* sin(x); %// Note the dot!

此行现在应该可以工作.

This line should now work.

这是给您的奖励,这是一个简单的示例.假设我有以下两个矩阵:

As a bonus for you, here's a simple example. Suppose I have these two matrices:

A = [1 2;
     3 4];

B = [4 3;
     2 1];

通过在MATLAB中执行A * B,您将得到:

By doing A * B in MATLAB, you get:

>> A * B

ans =

     8     5
    20    13

请注意,这将执行矩阵乘法.通过执行A .* B,这就是我得到的:

Note that this will perform a matrix multiplication. By doing A .* B, this is what I get:

>> A .* B

ans =

     4     6
     6     4

此语句的不同之处在于,将A中的一个元素乘以B中的对应元素. A的第一行和第一列乘以第一行,B的第一列,并在输出矩阵中的同一位置存储该结果.您可以跟随输出矩阵中的其他元素一起使用,这将为您提供相同的行为.还有其他逐个元素的操作,例如除法和求幂.加法和减法本质上是逐个元素的,因为根据定义,对矩阵执行这些操作是这种方式.

What's different with this statement is that one element in A is multiplied by the corresponding element in B. The first row and first column of A gets multiplied by the first row, first column of B, and the same location in the output matrix is where this result is stored. You can follow along with the other elements in the output matrix and it'll give you the same behaviour. There are other element-by-element operations, such as division and exponentiation. Addition and subtraction are inherently element-by-element, as performing these operations on matrices is by definition in this fashion.

要添加到@ellieadam的帖子中,请查看此MathWorks帖子,其中专门向您显示了关于矩阵和向量的各种运算,包括逐元素运算:

To add to @ellieadam's post, check this MathWorks post out that specifically shows you the various operations on matrices and vectors, including element-by-element operations:

http://www.mathworks.com/help/matlab/matlab_prog /array-vs-matrix-operations.html

这篇关于内部矩阵尺寸必须一致吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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