【问题标题】:Matlab: finding change points in cell arrayMatlab:在单元格数组中查找变化点
【发布时间】:2013-08-02 18:18:12
【问题描述】:

如果我有一个元胞数组

CELLS = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'}

如何找到元素变化位置的索引?

在此示例中,我正在寻找类似以下的输出:

CHANGES = 
        4 
        6 
        9
        10

【问题讨论】:

    标签: matlab cell-array


    【解决方案1】:

    对于字符串调用unique()find(diff(...))的位置索引的通用单元格数组:

    s = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'};
    [~,~,p] = unique(s)
    find(diff(p)==1)+1
    

    【讨论】:

      【解决方案2】:

      这样就可以了:

      CHANGES = find(diff(cell2mat(CELLS)))+1

      【讨论】:

      • 我不小心把我的问题简单化了一点:如果CELLS = {'AB','AB','AB','BC','BC','CD','CD','CD','DF','FG'}呢?
      猜你喜欢
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2013-08-06
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      相关资源
      最近更新 更多