使用简单矩阵乘法时出错 [英] Error using simple matrix multiplication

查看:169
本文介绍了使用简单矩阵乘法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一次简单的乘法运算中偶然发现了一个错误,这让我很惊讶.在这里发生了什么,我一直以为*仅用于矩阵乘法.

I stumbled upon an error during a simple multiplication that rather surprised me. What is happening here, I always assumed * was only for matrix multiplication.

x = 2;
y = zeros(1,4);
y(1) = 1 *x;
y(2) = x* 1;
y(3) = (x *1);
y(4) = x *1;
y
x *1

将给出以下输出:

y =

     2     2     2     1

Error: "x" was previously used as a variable,
conflicting with its use here as the name of a function or command.
See MATLAB Programming, "How MATLAB Recognizes Function Calls That Use Command Syntax" for details.

有人知道这里发生了什么吗?我当然确认x不是函数.

Does anyone understand what is going on here? Of course I verified that x is not a function.

推荐答案

这取决于间距.另请参见此处以获取详细说明还有一些示例,说明什么时候可能存在真正的歧义,但是基本上其中的前三个将按预期工作,而最后一个假定您正在尝试使用输入* 1调用函数x:

It depends on the spacing. See also here for a longer explanation and some examples of when you could have genuine ambiguity, but basically the first three of these will work as you expected, and the last will assume you are trying to call a function x with input *1:

x*1  
x * 1 
x* 1
x *1

如果您将输出分配给某个变量,而不管间距如何,都不会发生这种情况:

This doesn't happen if you assign the output to some variable, regardless of spacing:

y(2) = x *1
z = x *1
x = x *1

这篇关于使用简单矩阵乘法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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