元胞数组的向量化运算
这篇文章是由以下讨论触发的 关于元胞数组是否是“普通数组”以及矢量化不适用于元胞数组。
我想知道为什么 MATLAB 中没有实现以下矢量化语法,有什么反对它的:
>> {'hallo','matlab','world'} == 'matlab'
??? Undefined function or method 'eq' for input arguments of type 'cell'.
在内部它相当于
[{'hallo'},{'matlab'},{'world'}] == {'matlab'}
因为 MATLAB 知道何时进行转换,以下有效:
[{'hallo','matlab'},'world']
单元格数组是 指针数组。如果左侧和右侧都指向相同的对象,isequal('hallo','hallo')
按预期返回 true,那么为什么 MATLAB 仍然不允许最上面的示例?
我知道我可以使用strmatch
或cellfun
。
SUMMARY:
- 上例中矢量化所需的运算符
==
是eq
而不是isequal
(其他运算符是<
即lt
等) eq
是数字类型的内置参数,对于所有其他类型(如字符串),MATLAB 给出如下可以自由地重载此(和其他)运算符。- 因此,运算符向量化对于定义类型(如字符串)的元胞数组来说是可能的,但默认情况下对于任何类型都不是。
- 像
myFun( myString )
或myFun( myCellOfStrings )
这样的函数矢量化也是可能的,您只需在myFun
内部实现它即可。函数sin(val)
和sin(array)
的工作也不是通过巫术,而是因为这两种情况都是在内部实现的。
This post was triggered by following discussion on whether cell arrays are "normal arrays" and that vectorizaton does not work for cell arrays.
I wonder why following vectorization syntax is not implemented in MATLAB, what speaks against it:
>> {'hallo','matlab','world'} == 'matlab'
??? Undefined function or method 'eq' for input arguments of type 'cell'.
internally it would be equivalent to
[{'hallo'},{'matlab'},{'world'}] == {'matlab'}
because MATLAB knows when to cast, following works:
[{'hallo','matlab'},'world']
Cell array is an array of pointers. If both left and right side point to equal objects, isequal('hallo','hallo')
returns as expected true, then why MATLAB still does not allow topmost example?
I know I can use strmatch
or cellfun
.
SUMMARY:
- operator
==
which is required for vectorization in above example iseq
and notisequal
(other operators are<
which islt
, etc.) eq
is built-in for numeric types, for all other types (like strings) MATLAB gives as freedom to overload this (and other) operators.- operator vectorization is thus well possible with cell arrays of defined type (like string) but not by default for any type.
- function vectorization like
myFun( myString )
ormyFun( myCellOfStrings )
, is also possible, you have just to implement it internally inmyFun
. Functionssin(val)
andsin(array)
work also not by witchcraft but because both cases are implemented internally.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,
==
与不相同“nofollow noreferrer”>等于
。当您使用==
时调用的函数是eq
,并且每个的范围都不同。例如,在
eq(A,B)
中,如果B
是标量,则该函数检查A
的每个元素code> 与B
相等并返回一个逻辑向量。但是,
isequal(A,B)
检查A
在所有方面是否相同等于
B
。换句话说,MATLAB 无法区分A
和B
之间的区别。针对上面的示例执行此操作:我认为您在问题中真正想要问但没有问的是:
原因很简单:细胞本来就不适合这样的用途。当您开始考虑个别情况时,您可以很容易地看到为单元实现这样的函数会很快变得复杂。例如,考虑一下
您期望的答案是什么?一个合理的猜测
是公平的。但可以说,我不同意。现在我希望相等操作遍历嵌套单元格并返回
您可以不同意这种解释吗?也许不是。它同样有效,并且在某些情况下这就是您想要的行为。
这只是一个简单的例子。现在,在单元内添加嵌套单元、不同类型等的混合,并考虑处理每个极端情况。对于开发人员来说,实现这样一个可以让每个人都满意使用的功能很快就会成为一场噩梦。
因此,解决方案是重载该函数,仅实现您想要的特定功能,以便在您的应用程序中使用。 MATLAB 也提供了一种方法来实现这一点,即创建一个
@cell
目录并定义一个eq.m
以按照您想要的方式与单元格一起使用它。 Ramashalanka 在他的答案中证明了这一点。Firstly,
==
is not the same asisequal
. The function that gets called when you use==
iseq
, and the scope of each of those is different.For e.g., in
eq(A,B)
, ifB
is a scalar, the function checks each element ofA
for equality withB
and returns a logical vector.However,
isequal(A,B)
checks ifA
is identically equal toB
in all aspects. In other words, MATLAB cannot tell the difference betweenA
andB
. Doing this for the above example:I think what you really intended to ask in the question, but didn't, is:
Well, a simple reason is: Cells were not intended for such use. You can easily see how implementing such a function for cells can quickly get complicated when you start considering individual cases. For example, consider
What would you expect the answer to be? A reasonable guess would be
which is fair. But let's say, I disagree. Now I'd like the equality operation to walk down nested cells and return
Can you disagree with this interpretation? Perhaps not. It's equally valid, and in some cases that's the behavior you want.
This was just a simple example. Now add to this a mixture of nested cells, different types, etc. within the cell and think about handling each of those corner cases. It quickly becomes a nightmare for the developers to implement such a functionality that can be satisfactorily used by everyone.
So the solution is to overload the function, implementing only the specific functionality that you desire, for use in your application. MATLAB provides a way to do that too, by creating an
@cell
directory and defining aneq.m
for use with cells the way you want it. Ramashalanka has demonstrated this in his answer.有许多事情对于 MATLAB 来说似乎很自然,但他们却选择不这样做。也许他们不想考虑许多特殊情况(见下文)。您可以通过重载自己完成。如果您创建一个目录
@cell
并将以下内容放入新函数eq.m
中:那么您可以这样做,例如:
但是,即使我考虑了几种情况在我的简单函数中,有很多事情我没有考虑(检查单元格是否相同大小,根据单例检查多元素单元格等)。
我使用了
isequal
即使它是用eq
(即 ==)调用的,因为它处理{'hallo','matlab','world'} == ' matlab'
更好,但实际上我应该考虑更多情况。(编辑:我使函数稍微短一些,但效率较低)
There are many things that would seem natural for MATLAB to do that they have chosen not to. Perhaps they don't want to consider many special cases (see below). You can do it yourself by overloading. If you make a directory
@cell
and put the following in a new functioneq.m
:Then you can do, e.g.:
But, even though I considered a couple of cases in my simple function, there are lots of things I didn't consider (checking cells are the same size, checking a multi-element cell against a singleton etc etc).
I used
isequal
even though it's called witheq
(i.e. ==) since it handles{'hallo','matlab','world'} == 'matlab'
better, but really I should consider more cases.(EDIT: I made the function slightly shorter, but less efficient)
这并不是字符串所独有的。即使以下内容也不起作用:
元胞数组与“普通”数组不同:它们提供不同的语法、不同的语义、不同的功能,并且实现方式不同(额外的间接层)。
考虑元胞数组上的
==
是否是在逐个元素的基础上根据isequal
定义的。那么,上面的例子就没有问题了。但这又如何呢?在大多数情况下,由此产生的行为并不是很有用。那这个呢?
您如何定义这一点? (我至少可以想到三种不同的解释。)
那这个呢?
还是这个?
还是这个?
您可以添加各种特殊情况处理,但这会使语言不太一致、更复杂且更难以预测。
This is not unique to strings. Even the following does not work:
Cell arrays are not the same as "normal" arrays: they offer different syntax, different semantics, different capabilities, and are implemented differently (an extra layer of indirection).
Consider if
==
on cell arrays were defined in terms ofisequal
on an element-by-element basis. So, the above example would be no problem. But what about this?The resulting behaviour wouldn't be terribly useful in most circumstances. And what about this?
And how would you define this? (I can think of at least three different interpretations.)
And what about this?
Or this?
Or this?
You could add all sorts of special-case handling, but that makes the language less consistent, more complex, and less predictable.
出现这个问题的原因是:元胞数组可以在不同的元胞中存储不同类型的变量。因此不能为整个数组很好地定义运算符
==
。一个单元甚至有可能包含另一个单元,从而进一步加剧问题。想想
{4,'4',4.0,{4,4,4}} == '4'
。结果应该是什么?每种类型以不同的方式进行评估。The reason for this problem is: cell arrays can store different types of variables in different cells. Thus the operator
==
can't be defined well for the entire array. It is even possible for a cell to contain another cell, further exacerbating the problem.Think of
{4,'4',4.0,{4,4,4}} == '4'
. what should be the result? Each type evaluates in different way.