元胞数组的向量化运算

发布于 2024-11-17 13:28:54 字数 1362 浏览 2 评论 0原文

这篇文章是由以下讨论触发的 关于元胞数组是否是“普通数组”以及矢量化不适用于元胞数组。

我想知道为什么 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 仍然不允许最上面的示例?

我知道我可以使用strmatchcellfun

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 is eq and not isequal (other operators are < which is lt, 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 ) or myFun( myCellOfStrings ), is also possible, you have just to implement it internally in myFun. Functions sin(val) and sin(array) work also not by witchcraft but because both cases are implemented internally.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

围归者 2024-11-24 13:28:54

首先,==不相同“nofollow noreferrer”>等于。当您使用 == 时调用的函数是 eq,并且每个的范围都不同。

例如,在 eq(A,B) 中,如果 B 是标量,则该函数检查 A每个元素code> 与 B 相等并返回一个逻辑向量。

eq([2,5,4,2],2)

ans =

     1     0     0     1

但是,isequal(A,B) 检查A 在所有方面是否相同等于B。换句话说,MATLAB 无法区分 AB 之间的区别。针对上面的示例执行此操作:

isequal([2,5,4,2],2)

ans =

     0

我认为您在问题中真正想要问但没有问的是:

“为什么没有为元胞数组定义==?”

原因很简单:细胞本来就不适合这样的用途。当您开始考虑个别情况时,您可以很容易地看到为单元实现这样的函数会很快变得复杂。例如,考虑一下

{2,5,{4,2}}==2

您期望的答案是什么?一个合理的猜测

ans = {1,0,0}

是公平的。但可以说,我不同意。现在我希望相等操作遍历嵌套单元格并返回

ans = {1,0,{0,1}}

您可以不同意这种解释吗?也许不是。它同样有效,并且在某些情况下这就是您想要的行为。

这只是一个简单的例子。现在,在单元内添加嵌套单元、不同类型等的混合,并考虑处理每个极端情况。对于开发人员来说,实现这样一个可以让每个人都满意使用的功能很快就会成为一场噩梦。

因此,解决方案是重载该函数,仅实现您想要的特定功能,以便在您的应用程序中使用。 MATLAB 也提供了一种方法来实现这一点,即创建一个 @cell 目录并定义一个 eq.m 以按照您想要的方式与单元格一起使用它。 Ramashalanka 在他的答案中证明了这一点。

Firstly, == is not the same as isequal. The function that gets called when you use == is eq, and the scope of each of those is different.

For e.g., in eq(A,B), if B is a scalar, the function checks each element of A for equality with B and returns a logical vector.

eq([2,5,4,2],2)

ans =

     1     0     0     1

However, isequal(A,B) checks if A is identically equal to B in all aspects. In other words, MATLAB cannot tell the difference between A and B. Doing this for the above example:

isequal([2,5,4,2],2)

ans =

     0

I think what you really intended to ask in the question, but didn't, is:

"Why is == not defined for cell arrays?"

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

{2,5,{4,2}}==2

What would you expect the answer to be? A reasonable guess would be

ans = {1,0,0}

which is fair. But let's say, I disagree. Now I'd like the equality operation to walk down nested cells and return

ans = {1,0,{0,1}}

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 an eq.m for use with cells the way you want it. Ramashalanka has demonstrated this in his answer.

孤君无依 2024-11-24 13:28:54

有许多事情对于 MATLAB 来说似乎很自然,但他们却选择不这样做。也许他们不想考虑许多特殊情况(见下文)。您可以通过重载自己完成。如果您创建一个目录 @cell 并将以下内容放入新函数 eq.m 中:

function c = eq(a,b)
if iscell(b) && ~iscell(a)
    c = eq(b,a);
else
    c = cell(size(a));
    for n = 1:numel(c)
        if iscell(a) && iscell(b)
            c{n} = isequal(a{n},b{n});
        else
            c{n} = isequal(a{n},b);
        end
    end
end

那么您可以这样做,例如:

>> {'hallo','matlab','world'} == 'matlab'
ans =     [0]    [1]    [0]

>> {'hallo','matlab','world'} == {'a','matlab','b'}
ans =     [0]    [1]    [0]

>> {'hallo','matlab','world'} == {'a','dd','matlab'}
ans =     [0]    [0]    [0]

>>  { 1, 2, 3 } == 2
ans =     [0]    [1]    [0]

但是,即使我考虑了几种情况在我的简单函数中,有很多事情我没有考虑(检查单元格是否相同大小,根据单例检查多元素单元格等)。

我使用了 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 function eq.m:

function c = eq(a,b)
if iscell(b) && ~iscell(a)
    c = eq(b,a);
else
    c = cell(size(a));
    for n = 1:numel(c)
        if iscell(a) && iscell(b)
            c{n} = isequal(a{n},b{n});
        else
            c{n} = isequal(a{n},b);
        end
    end
end

Then you can do, e.g.:

>> {'hallo','matlab','world'} == 'matlab'
ans =     [0]    [1]    [0]

>> {'hallo','matlab','world'} == {'a','matlab','b'}
ans =     [0]    [1]    [0]

>> {'hallo','matlab','world'} == {'a','dd','matlab'}
ans =     [0]    [0]    [0]

>>  { 1, 2, 3 } == 2
ans =     [0]    [1]    [0]

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 with eq (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)

你丑哭了我 2024-11-24 13:28:54

这并不是字符串所独有的。即使以下内容也不起作用:

{ 1, 2, 3 } == 2

元胞数组与“普通”数组不同:它们提供不同的语法、不同的语义、不同的功能,并且实现方式不同(额外的间接层)。

考虑元胞数组上的 == 是否是在逐个元素的基础上根据 isequal 定义的。那么,上面的例子就没有问题了。但这又如何呢?

{ [1 0 1], [1 1 0] } == 1

在大多数情况下,由此产生的行为并不是很有用。那这个呢?

1 == { 1, 2, 3 }

您如何定义这一点? (我至少可以想到三种不同的解释。)

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

那这个呢?

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

还是这个?

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

还是这个?

{ 1, 2, 3 } == { 4, 5 }

您可以添加各种特殊情况处理,但这会使语言不太一致、更复杂且更难以预测。

This is not unique to strings. Even the following does not work:

{ 1, 2, 3 } == 2

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 of isequal on an element-by-element basis. So, the above example would be no problem. But what about this?

{ [1 0 1], [1 1 0] } == 1

The resulting behaviour wouldn't be terribly useful in most circumstances. And what about this?

1 == { 1, 2, 3 }

And how would you define this? (I can think of at least three different interpretations.)

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

And what about this?

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

Or this?

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

Or this?

{ 1, 2, 3 } == { 4, 5 }

You could add all sorts of special-case handling, but that makes the language less consistent, more complex, and less predictable.

森林迷了鹿 2024-11-24 13:28:54

出现这个问题的原因是:元胞数组可以在不同的元胞中存储不同类型的变量。因此不能为整个数组很好地定义运算符==。一个单元甚至有可能包含另一个单元,从而进一步加剧问题。

想想 {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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文