matlab - 在两个元胞数组 22124x1 中查找重复的字符串值

标签 matlab

我有一个单元格 22124x1,它包含重复的值,我想知道这些值重复了多少次以及它们的索引

第一个单元格包含这些值 Datacell=

  '221853_s_at'
  '221971_x_at'
  '221971_x_at'
  '221971_x_at'
  '221971_x_at'
  '222031_at'
  '222031_at'
  '31637_s_at'
  '37796_at'
  '38340_at'

符号单元格:

 'OR1D4 '
  ' OR1D5'
   ' UTP14C'
    'GTF2H2 '
   'ZNF324B '
  ' LOC644504'
   'JMJD7 '
  'ZNF324B '
  'JMJD7-PLA2G4B'
  ' OR2A5 '
   'OR1D4 '

例如,我想要像这样的单元格 1 的输出

ID               duplicated       index
'221853_s_at'       1               1
'221971_x_at'       4             {2:5,1}

我尝试使用 unique 但它不起作用。任何帮助将不胜感激

最佳答案

以视觉上令人愉悦的方式生成索引并不一定是一项简单的工作。如果您假设 d 已排序,事情就会变得更简单。

使用accumarray的替代方案:

d = {'221853_s_at'; '221971_x_at'; '221971_x_at';  '221971_x_at'; '221971_x_at'; ...
     '222031_at'; '222031_at'; '31637_s_at'; '37796_at'; '38340_at' ...
     };
d = sort(d); % Sort to make indices easier

% Find unique strings and their locations
[uniquestrings, ~, stringbin] = unique(d);
counts = accumarray(stringbin, 1);

repeatidx = find(counts - 1 > 0);
repeatedstrings = uniquestrings(repeatidx);
repeatcounts = counts(repeatidx) - 1;

% Find where string repeats start
startidx = find([true; diff(stringbin) > 0]);
repeatstart = startidx(repeatidx);
repeatend = startidx(repeatidx + 1) - 1;

% Generate table, requires R2013b or newer
t = table(repeatedstrings, repeatcounts, repeatstart, repeatend, ...
          'VariableNames', {'ID', 'Duplicated', 'StringStart', 'StringEnd'} ...
          );

其产量:

t = 

         ID          Duplicated    StringStart    StringEnd
    _____________    __________    ___________    _________

    '221971_x_at'    3             2              5        
    '222031_at'      1             6              7        

关于matlab - 在两个元胞数组 22124x1 中查找重复的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013516/

相关文章:

matlab - 如何在 MATLAB 中将自定义颜色范围与颜色条组合?

matlab - 如何在 Matlab 中沿对角线重复一个向量

performance - Matlab中的快速矩阵乘法

matlab - 如何使用 Graph Cuts 分割灰度二维图像中的对象?

c++ - 如何使用gsl在C++上实现左矩阵除法

matlab - 在MATLAB中显示文本文档

regex - 替换 Simulink 模块名称中的非法字符

matlab - 如果出错,请在 dbstop 期间阻止 Matlab 进入内置函数

matlab - 创建和初始化元胞数组

database - 如何在 matlab 中检查 native ODBC 连接的状态?