为什么单元格数组中的尾部逗号有效的Matlab语法? [英] Why is a trailing comma in a cell array valid Matlab syntax?

查看:165
本文介绍了为什么单元格数组中的尾部逗号有效的Matlab语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我很惊讶地发现了

A = {1,2,3}

B = {1,2,3,}

都是MATLAB中的有效语法.我本来希望第二条语句会产生错误.据我所知,它们产生相同的单元格数组(all([A{:}]==[B{:}])返回true).

are both valid syntax in MATLAB. I would have expected the second statement to yield an error. As best as I can tell, they produce identical cell arrays (all([A{:}]==[B{:}]) returns true).

是否存在允许使用第二种语法的原因?这是解析器中的错误吗? AB真的一样吗?

Is there a reason the second syntax is allowed? Is this a bug in the parser? Are A and B truly the same?

有趣的是,不允许以下内容:

Intriguingly, the following is not allowed:

C = {1,2,3,,,}

推荐答案

这些是更多的猜测,而不是答案.

These are more guesses, rather than an answer.

可以检查 符号参考 并发现逗号, 可以用作

One could check the Symbol reference and find that the comma , can be used as

命令或语句分隔符

要在同一行上输入多个MATLAB命令或语句, 用逗号分隔每个命令或语句:

To enter more than one MATLAB command or statement on the same line, separate each command or statement with a comma:

for k = 1:10, sum(A(k)), end

在行

B = {1,2,3,}

因此,期望在3之后有一条语句,只有},这表示单元格数组结尾是有效的语句.

therefore an statement after 3 is expected, there is just }, which means end of cell array, a valid statement.

分号; 具有三种官方用法:

The semicolon ; has three official usages:

阵列行分隔符

在方括号内使用时创建新数组或并置 现有数组,分号会在数组中创建新行:

When used within square brackets to create a new array or concatenate existing arrays, the semicolon creates a new row in the array:

A = [5, 8; 3, 4]

输出抑制

当放在命令末尾时,分号告诉MATLAB不要 显示该命令的任何输出.在此示例中,MATLAB不会 显示最终的100 x 100矩阵:

When placed at the end of a command, the semicolon tells MATLAB not to display any output from that command. In this example, MATLAB does not display the resulting 100-by-100 matrix:

A = ones(100, 100);

命令或语句分隔符

就像逗号运算符一样,您可以在上输入多个MATLAB命令 通过用分号分隔每个命令来分隔一行. MATLAB抑制 那些以分号终止的命令的输出,并显示 命令输出以逗号结尾.

Like the comma operator, you can enter more than one MATLAB command on a line by separating each command with a semicolon. MATLAB suppresses output for those commands terminated with a semicolon, and displays the output for commands terminated with a comma.

在此示例中,变量A和C的赋值以终止 分号,因此不显示.因为对B的赋值是 以逗号结尾,将显示此命令的输出:

In this example, assignments to variables A and C are terminated with a semicolon, and thus do not display. Because the assignment to B is comma-terminated, the output of this one command is displayed:

A = 12.5; B = 42.7, C = 1.25;

所以在这一行

x = {1,2,3,;5,6,7}

它紧随3,之后的有效语句 Array Row Separator .之后,将需要一个新的语句,在这种情况下为双 5 .有效.

it follows the valid statement Array Row Separator after 3,. Afterwards a new statement is expected, which in this case is the double 5. Valid.

现在考虑一下情况

x = {1,2,3,;;;;4,5,6;;;}

3,之后紧跟在语句 Array Row Separator 之后,并且此后的语句大概是 之后,下一条语句才出现. 毫无意义,因为Matlab告诉您:多余的分号是不必要的.-但有效.

As above after 3, follows the statement Array Row Separator, and the statement after that is presumably the null statement - NOP borrowed from some underlying program core written in C, which basically means: do nothing. So after 3,; follows three times "do nothing", before there comes the next statement. Makes no sense, as Matlab is telling you: Extra semicolon is unnecessary. - but is valid.

它还允许您实现无意义的事情,例如:

It also allows you pointless things like:

if true
    ;
end


这也许也是 的原因


And this is presumably also the reason why

C = {1,2,3,,,} 

返回错误,因为逗号,不是 null语句,但是在第一个逗号之后会有一个预期的语句.

returns an error, because the comma , isn't a null statement, but after the first comma there is a statement expected.

最重要的是:它看起来很奇怪,但实际上对我来说似乎是逻辑,因为Matlab在内部使用了大量的C代码并考虑了null语句,因此所提及的一切都是有效的语法.

The bottom line: it looks weird, but actually seems logic to me, as Matlab uses a lot of C-Code internally and considering the null statement, everything mentioned is valid syntax.

其他语言呢?

在Python中像x = [1,2,3,;;;;4,5,6;;;]一样使用的分号都是无效的,即使在预期的Matlab克隆 numpy 中也是如此,除非用这种不常见的语法a = np.matrix('1,2,3;4,5,6')包装.

Semi-colons used like x = [1,2,3,;;;;4,5,6;;;] in Python are invalid, even in the intended Matlab clone numpy, unless wrapped into this uncommon syntax a = np.matrix('1,2,3;4,5,6').

a = np.matrix('1,2,3,;;;;4,5,6;;;')

也会抛出错误,因为;在任何情况下都被解释为数组行分隔符,这使编译器抱怨行大小不一致.

would throw an error as well, as ; is interpreted as Array Row Separator in any case, which makes the compiler complain about inconsitent row sizes.

但是

x = [1,2,3,]

中所述,

PythonIronPython中也是有效的语法,在VBScriptLua中也是如此. mlepage的答案 .这些语言有什么共同点?它们是在运行时解释的所有(或多或少)脚本语言.不只是Matlab.因此,OP的激动依然存在.

is also valid syntax in Python and IronPython, as it is in VBScript and Lua as mentioned in mlepage's answer. What do all these languages have in common? They are all (more or less) scripting languages interpreted during runtime. It's not just Matlab. The excitement of the OP therefore remains without cause.

这篇关于为什么单元格数组中的尾部逗号有效的Matlab语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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