eq对于非相等列表返回true [英] eq returns true for non equal lists

查看:73
本文介绍了eq对于非相等列表返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一段奇怪的代码来调试,我认为应该抛出一个异常,但是却产生了完全奇怪的结果.将其简化为这两行:

I got a strange piece of code to debug which to my opinion should throw an exception but instead it produced totally odd results. Reduced it to these two lines:

EDU>> A={0,0}

A = 

    [0]    [0]

EDU>> A{1:2}==A{2:1}

ans =

     1

为什么两个非等号逗号分隔列表的比较是正确的?

Why is the comparison of two non equal comma separated lists true?

推荐答案

代码行A{1:2}==A{2:1}不会检查两个逗号分隔的列表是否相等,因为2:1是一个空数组.我认为预期的索引是2:-1:1;这将创建一个逗号分隔的列表,但由于==无法处理该列表,因此也会引发错误.

The line of code A{1:2}==A{2:1} is not checking the equality of two comma-separated lists because 2:1 is an empty array. I think the intended indexing was 2:-1:1; this will create a comma-separated list but also throw an error since == cannot handle the list.

但是,在我的脑海中,A{1:2}==A{2:1}会产生任何有效的输出,这很奇怪.该代码字面意思是A{1:2} == A{[]},问题是"A{[]}是什么?".根据我的MATLAB R2014b,没有任何意义,但即使是带有空索引的简单double数组也将返回空double.我想{}检索到的实际内容没什么,是的.

However, it is odd that A{1:2}==A{2:1} produces a valid output of any kind in my mind. The code is literally saying A{1:2} == A{[]}, and the question is "what is A{[]}?" According to my MATLAB R2014b, nothing, which makes some sense, but even a simple double array with an empty index returns an empty double. I guess the actual content, which is what is retreived by { and }, is nothing so, yeah.

但是MATLAB如何产生true的答案? 在命令窗口中考虑以下代码:

But then how is MATLAB producing the answer of true? Consider the following code from the command window:

>> A = {0,0}; A{1:2} == A{[]}
ans =
     1

>> A = {0,1}; A{1:2} == A{[]}
ans =
     0

据此,我推测MATLAB将逗号分隔的列表作为eq的前两个参数,并在其后未附加A{[]}并将其简单解释为

From that, I surmise that MATLAB places the comma-separated list as the first two arguments to eq and appends A{[]} nothing to it and interpret it simply as

eq(0,0,A{[]})
eq(0,1,A{[]})

显然是有效的语法(eq(a,b,)不是).对单元数组的元素执行二进制操作非常有趣.这也可以:

which is, apparently, valid syntax (eq(a,b,) is not). It is very interesting for a binary operation on elements of a cell array. This also works:

>> A = {[2,3],[3,2]};
>> A{1:2} .* A{[]}
ans =
     6     6

>> A{1:2} ./ A{[]}
ans =
    0.6667    1.5000

只是为了好玩,因为我发现这很有趣:

And just for fun, because I'm finding this quite interesting:

>> A = {rand(2),rand(2,1)};
>> A{1:2} \ A{[]}
ans =
    0.8984
   -0.7841


但是我想这是有道理的.解析器先找到一个令牌,然后是一个中缀运算符,然后是另一个令牌.它将infix运算符解析为其函数,然后将左右标记依次放入参数列表中. 我想我对无"的存在感到奇怪.尽管这将解释[1,2,3,]如何是有效的语法.


But I guess it makes sense. The parser finds a token, followed by an infix operator, followed by another token. It resolves the infix operator to its function, and then places the left and right tokens into the argument list in turn. I guess I just find it a odd about the existence of "nothing"; although that would explain how [1,2,3,] is valid syntax.

也就是说,我确定这是语言的怪癖,而不是错误或功能.

That said, I'm sure this is a quirk of the language and not a bug nor a feature.

当然,要知道实际发生的唯一方法是对MATLAB如何解释单元格数组扩展和运算符的应用有深入的了解.当然,我没有这种经验,也没有要求的资源(我想像).

Of course, the only way to know what is actually going on is to have an intimate knowledge of how MATLAB is interpreting the cell array expansion and application of the operator. Of course, I do not have this experience nor the source required (I'd imagine).

这篇关于eq对于非相等列表返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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