细胞阵列的向量化运算 [英] Vectorized operations on cell arrays

查看:94
本文介绍了细胞阵列的向量化运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该帖子是由触发的关于单元格数组是否为普通数组"以及该矢量化不适用于单元格数组.

This post was triggered by following discussion on whether cell arrays are "normal arrays" and that vectorizaton does not work for cell arrays.

我想知道为什么以下矢量化语法没有在MATLAB中实现,反对它的原因:

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'.

内部等同于

[{'hallo'},{'matlab'},{'world'}] == {'matlab'}

由于MATLAB知道何时进行转换,因此可以进行以下工作:

because MATLAB knows when to cast, following works:

[{'hallo','matlab'},'world']

单元格数组是指针数组.如果左右两边都指向相同的对象,则isequal('hallo','hallo')返回预期的true,那么为什么MATLAB仍然不允许最上面的示例?

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?

我知道,我可以使用strmatchcellfun.

摘要:

    上例中进行矢量化所需的
  • 运算符==eq而不是isequal(其他运算符是<,它是lt,依此类推)
  • eq内置用于数字类型,对于所有其他类型(例如字符串),MATLAB可以自由地重载此(和其他)运算符.
  • 因此,
  • 操作符向量化很可能使用定义类型(例如字符串)的单元格数组,但是默认情况下对于任何类型的单元格都没有.
  • myFun( myString )myFun( myCellOfStrings )这样的
  • 功能矢量化也是可能的,您只需在myFun内部实现即可.函数sin(val)sin(array)也不是通过巫术来工作的,而是因为这两种情况都是在内部实现的.
  • 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.

推荐答案

首先,== eq ,并且每个范围都不同.

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.

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

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

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

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:

为什么未为单元阵列定义==?"

嗯,一个简单的原因是:电池不适合用于此类用途.当您开始考虑个别情况时,您可以轻松地看到如何为单元实现这种功能会很快变得复杂.例如,考虑

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.

因此解决方案是重载功能,仅实现您想要的 特定功能,以供您在应用程序中使用. MATLAB也提供了一种方法,方法是创建一个@cell目录并定义一个eq.m以供所需的单元格使用. Ramashalanka在他的答案中对此进行了证明.

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.

这篇关于细胞阵列的向量化运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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