MATLAB 中 [] 和 [1x0] 之间的区别
我在 MATLAB 中有一个循环,它使用以下条目填充工作区(2011b、Windows 7、64 位)中的元胞数组:
my_array =
[1x219 uint16]
[ 138]
[1x0 uint16] <---- row #3
[1x2 uint16]
[1x0 uint16]
[] <---- row #6
[ 210]
[1x7 uint16]
[1x0 uint16]
[1x4 uint16]
[1x0 uint16]
[ 280]
[]
[]
[ 293]
[ 295]
[1x2 uint16]
[ 298]
[1x0 uint16]
[1x8 uint16]
[1x5 uint16]
请注意,某些条目包含 []
,如行 # 6
,而其他人持有 [1x0]
项目,如行 #3
中。
- 它们之间有任何区别吗? (除了 MATLAB 以不同的方式显示它们之外)。 MATLAB 在内存中表示它们的方式有什么不同吗?
- 如果差异仅在于 MATLAB 内部如何表示它们,为什么程序员应该意识到这种差异? (即为什么以不同的方式显示它们?)。这是一个(无害的)错误吗?或者知道这些数组以不同的方式表示是否有任何好处?
I have a loop in MATLAB that fills a cell array in my workspace (2011b, Windows 7, 64 bit) with the following entries:
my_array =
[1x219 uint16]
[ 138]
[1x0 uint16] <---- row #3
[1x2 uint16]
[1x0 uint16]
[] <---- row #6
[ 210]
[1x7 uint16]
[1x0 uint16]
[1x4 uint16]
[1x0 uint16]
[ 280]
[]
[]
[ 293]
[ 295]
[1x2 uint16]
[ 298]
[1x0 uint16]
[1x8 uint16]
[1x5 uint16]
Note that some entries hold []
, as in row #6
, while others hold [1x0]
items, as in row #3
.
- Is there any difference between them? (other than the fact that MATLAB displays them differently). Any differences in how MATLAB represents them in memory?
- If the difference is only about how MATLAB internally represents them, why should the programmer be aware of this difference ? (i.e. why display them differently?). Is it a (harmless) bug? or is there any benefit in knowing that such arrays are represented differently?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在大多数情况下(见下文的例外)没有真正的区别。两者都被视为“空”,因为至少一个维度具有大小为 0。但是,我不会将此称为错误,因为作为程序员,您可能希望在某些情况下看到此信息。
举例来说,您有一个二维矩阵,您想要索引一些行和一些列以提取到一个较小的矩阵中:
请注意,空结果仍然告诉您一些信息,特别是您尝试索引 2 行原始矩阵。如果结果只是显示
[]
,您将不知道它是否为空,因为您的行索引为空,或者您的列索引为空,或者两者都为空。警告...
在某些情况下,定义为
[]
的空矩阵(即其所有维度均为 0)可能会给出与仍具有一些非零维度的空矩阵不同的结果。例如,在处理不同类型的空矩阵时,矩阵乘法可以给您不同的(并且有些不直观的)结果。让我们考虑这 3 个空矩阵:现在,让我们尝试以不同的方式将它们相乘:
通过将两个空矩阵相乘得到一个非空矩阵可能足以让您头疼,但这也有点道理,因为结果仍然不实际上包含任何东西(即它的值为 0)。
In most cases (see below for an exception) there is no real difference. Both are considered "empty", since at least one dimension has a size of 0. However, I wouldn't call this a bug, since as a programmer you may want to see this information in some cases.
Say, for example, you have a 2-D matrix and you want to index some rows and some columns to extract into a smaller matrix:
Note that the empty result still tells you some information, specifically that you tried to index 2 rows from the original matrix. If the result just showed
[]
, you wouldn't know if it was empty because your row indices were empty, or your column indices were empty, or both.The Caveat...
There are some cases when an empty matrix defined as
[]
(i.e. all of its dimensions are 0) may give you different results than an empty matrix that still has some non-zero dimensions. For example, matrix multiplication can give you different (and somewhat non-intuitive) results when dealing with different kinds of empty matrices. Let's consider these 3 empty matrices:Now, let's try multiplying these together in different ways:
Getting a non-empty matrix by multiplying two empty matrices is probably enough to make your head hurt, but it kinda makes sense since the result still doesn't really contain anything (i.e. it has a value of 0).
连接矩阵时,公共维度必须匹配。
如果操作数之一为空时不匹配,目前并不是错误,但您确实会收到一条令人讨厌的警告,表明未来版本可能会更严格。
示例:
When concatenating matrices, the common dimension has to match.
It's not currently an error if it doesn't match when one of the operands is empty, but you do get a nasty warning that future versions might be more strict.
Examples:
另一个区别在于两个版本的empty 的内部表示。特别是当将同一类的对象捆绑在一个数组中时。
假设您有一个虚拟类:
如果您尝试从空开始一个数组并将其增长为 A 对象的数组:
那么您会得到:
但如果您这样做:
那么一切都很好。
我希望这可以补充之前给出的解释。
Another difference is in the internal representation of both versions of empty. Especially when it comes to bundle together objects of the same class in an array.
Say you have a dummy class:
If you try to start an array from empty and grow it into an array of A objects:
Then you get:
But if you do:
Then all is fine.
I hope this adds to the explanations given before.
如果连接和乘法还不足以担心,那么仍然存在循环。以下是观察差异的两种方法:
1.循环遍历变量 size
将打印
'yes'
,如果将size
替换为length
则根本不会打印任何内容。如果这还不算意外,也许下一个就会令人意外。
2. 使用 for 循环迭代空矩阵
将打印:
最后对您的两个问题给出一个简洁的答案:
If concatenation and multiplication is not enough to worry about, there is still looping. Here are two ways to observe the difference:
1. Loop over the variable size
Will print
'yes'
, if you replacesize
bylength
it will not print anything at all.If this is not a surprise, perhaps the next one will be.
2. Iterating an empty matrix using a for loop
Will print:
To conclude with a concise answer to both of your questions: