我有一个单元格 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/