简体   繁体   English

为什么 Octave 打印“尺寸不匹配”而 MATLAB 不打印?

[英]Why does Octave print “dimensions mismatch” whereas MATLAB does not?

I am trying to run a MATLAB code in Octave but got stuck at the following point:我正在尝试在 Octave 中运行 MATLAB 代码,但遇到了以下问题:

I is an empty matrix, dimensions 0x4, I是一个空矩阵,维度为 0x4,

a = 2;

The command, belonging to a for-loop, is:属于 for 循环的命令是:

I = [I a];

MATLAB output: I = 2 MATLAB 输出: I = 2

Octave output: "horizontal dimensions mismatch (0x4 vs 1x1)"八度输出:“水平尺寸不匹配(0x4 vs 1x1)”

I have found a way to work around this error but I would also like to understand: Why does MATLAB accept those different dimensions whereas Octave prints an error?我找到了解决此错误的方法,但我也想了解:为什么 MATLAB 接受这些不同的维度,而 Octave 会打印错误? Is there a different definition regarding empty matrices and extending those?关于空矩阵和扩展它们是否有不同的定义? (Specially because it is not a "normal" empty matrix but a 0x4 empty matrix?) (特别是因为它不是一个“正常”的空矩阵,而是一个 0x4 的空矩阵?)

Matlab issues a warning, alerting you to the fact that this will become an error in future releases: Matlab 发出警告,提醒您这将在未来版本中成为错误的事实:

>> I = magic(4);
>> I(1:4,:) = []
I =
   Empty matrix: 0-by-4
>> [I 2]
Warning: This concatenation operation includes an empty array with an incorrect number of rows.
Concatenation including empty arrays will require all arrays to have the same number of rows in a future release. 

ans =
 2

Same code on Octave: Octave 上的相同代码:

>> I = magic(4);
>> I(1:4,:)=[]
I = [](0x4)

>> [I 2]
error: horizontal dimensions mismatch (0x4 vs 1x1)

So essentially it's the same issue, except Matlab allows it with a warning for the time being, and is being slightly more informative as to which dimension is actually at fault here, whereas octave is stricter about it and hopes you figure out what it meant 😛.所以本质上它是相同的问题,除了 Matlab 暂时允许它发出警告,并且关于哪个维度实际上有问题的信息稍微多一些,而八度音程对此更严格,希望你弄清楚它的意思😛 . But in essence the behaviour is the same.但本质上,行为是相同的。

It is also very reasonable behaviour, since attempting to concatenate two matrices of different sizes / dimensions is more likely to have come from a bug rather than intended behaviour, even if one of the arrays has become empty in the process, so matlab is wise to go down the octave path here (so to speak).这也是非常合理的行为,因为尝试连接两个不同大小/维度的矩阵更有可能来自错误而不是预期行为,即使其中一个数组在此过程中变为空,因此 matlab 是明智的在这里走八度音程(可以这么说)。


PS. 附注。 Note that in this scenario, something like [I;2 2 2 2] is perfectly valid and correct code on both interpreters: ie you're concatenating vertically a 4-column matrix with one row to a 4-column matrix with no rows, hence the number of columns is consistent. 请注意,在这种情况下,像[I;2 2 2 2]代码在两个解释器上都是完全有效且正确的代码:即,您将具有一行的 4 列矩阵垂直连接到没有行的 4 列矩阵,因此列数是一致的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM