Removing redundant rows where not every row has the same number of elements
2 ビュー (過去 30 日間)
古いコメントを表示
Hello,
I have data that often looks like this:
"HIST1H2BC" "K13"
"HIST1H2BC" "K13;K16"
"HIST1H2BC" "K16"
"HIST1H2BH" "K13"
"HIST1H2BH" "K13;K16"
"HIST1H2BH" "K16"
"HIST1H2BO" "K13;K16"
"HIST1H2BO" "K16"
"HIST2H2BE" "K13;K16"
"HIST2H2BE" "K16"
I have been trying to code a function that splits the second columns at the ';' and then removes any rows for which every element is contained in another row, which would hopefully yield something like this:
"HIST1H2BC" "K13" "K16"
"HIST1H2BH" "K13" "K16"
"HIST1H2BO" "K13" "K16"
"HIST2H2BE" "K13" "K16"
All of the solutions I have tried have been very excessive and difficult to wrap my head around.
Thank you in advance!
0 件のコメント
回答 (2 件)
Chunru
2021 年 7 月 30 日
編集済み: Chunru
2021 年 7 月 30 日
x = ["HIST1H2BC" "K13"
"HIST1H2BC" "K13;K16"
"HIST1H2BC" "K16"
"HIST1H2BH" "K13"
"HIST1H2BH" "K13;K16"
"HIST1H2BH" "K16"
"HIST1H2BO" "K13;K16"
"HIST1H2BO" "K16"
"HIST2H2BE" "K13;K16"
"HIST2H2BE" "K16"
"1H1D" "K137"
"1H1D" "K137|K138"
"1H1D" "K138"
"1H1D" "K136"
"1H1E" "K136|K137"
"1H1E" "K137"];
s = split(x(1, 2), {';', '|'});
y = {x(1, 1), s'};
for i=2:size(x, 1)
s = split(x(i, 2), {';', '|'});
[lix, locy] = ismember(x(i, 1), [y{:, 1}]);
if ~lix
% new entry
y =[ y; {x(i, 1) ,s'}];
else
[lis, loc] = ismember(s, y{locy, 2});
y{locy, 2} = [y{locy, 2} s(~lis)'];
end
end
y
0 件のコメント
KSSV
2021 年 7 月 30 日
str = ["HIST1H2BC" "K13"
"HIST1H2BC" "K13;K16"
"HIST1H2BC" "K16"
"HIST1H2BH" "K13"
"HIST1H2BH" "K13;K16"
"HIST1H2BH" "K16"
"HIST1H2BO" "K13;K16"
"HIST1H2BO" "K16"
"HIST2H2BE" "K13;K16"
"HIST2H2BE" "K16"];
iwant = strings([],3) ;
count = 0 ;
for i = 1:length(str)
s = strsplit(str(i,2),';') ;
if length(s) == 2
count = count+1 ;
iwant(count,:) = [str(i,1) s] ;
end
end
8 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!