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 iseqand notisequal(other operators are<which islt, etc.) eqis 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.
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), ifBis a scalar, the function checks each element ofAfor equality withBand returns a logical vector.However,
isequal(A,B)checks ifAis identically equal toBin all aspects. In other words, MATLAB cannot tell the difference betweenAandB. 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
@celldirectory and defining aneq.mfor use with cells the way you want it. Ramashalanka has demonstrated this in his answer.