代码之家  ›  专栏  ›  技术社区  ›  Andi

检查单元格是否相等

  •  1
  • Andi  · 技术社区  · 7 年前

    我在寻找一种优雅的方法来计算,细胞阵列中的细胞是否完全相等 equaln . 以下是一个玩具示例:

    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];
    

    我想检查一下单元格中的内容 cellarray{1,1,:} 可以认为是平等的。实际上,这个矩阵的第三维尺寸是1000。

    2 回复  |  直到 7 年前
        1
  •  4
  •   gnovice    7 年前

    一通电话 isequal comma-separated list 要测试的参数,应该是您所需要的全部:

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

    如果你想治疗 NaN 如果值相等(默认情况下不是),则只需使用 isequaln 相反。

        2
  •  0
  •   Muttley    7 年前
    c = cellarray(1,1,:);
    allTheSameValues = (nnz(bsxfun(@minus, cell2mat(c), cell2mat(c(1)))) == 0)