Search code examples
matlabequalitycell-array

Check cells for equality


I am looking for an elegant way to figure out, whether cells in a cell array are totally equal using equaln. Here's a toy example:

cellarray{1,1,1} = [true, true, true];
cellarray{1,1,2} = [true, true, true];
cellarray{1,1,3} = [true, true, true];

cellarray{1,2,1} = [true, false, false];
cellarray{1,2,2} = [true, true, false];
cellarray{1,2,3} = [false, false, false];

I would like to check if the content in cells cellarray{1,1,:} can considered to be equal. In reality, the size of the third dimension of this matrix is 1000.


Solution

  • A single call to isequal, passing a comma-separated list of arguments to test, should be all you need:

    >> isequal(cellarray{1,1,:})
    
    ans =
    
      logical
    
       1
    
    >> isequal(cellarray{1,2,:})
    
    ans =
    
      logical
    
       0
    

    If you want to treat NaN values as equal (they aren't by default) then just use isequaln instead.