How to conditionally merge rows in a table
12 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a quick question - I have the following table T and I would like to merge the rows where the values of the variables lat, lon and id are identical and where only the values of the key variable are different. The desired output is shown.
lat = [45.67, 45.67, 56.89, 78.61]';
lon = [-66.45, -66.45, -65, -67]';
id = [202, 202, 201, 200]';
key = {'A', 'B', 'C', 'C'}';
T = table(lat, lon, id, key)
[C, ia, ic] = unique(T(:, 1:3),'rows')
% desired output:
lat = [45.67, 56.89, 78.61]';
lon = [-66.45, -65, -67]';
id = [202, 201, 200]';
key = {'A, B', 'C', 'C'}';
desired_output = table(lat, lon, id, key)
Thank you,
0 件のコメント
採用された回答
Stephen23
2021 年 1 月 8 日
編集済み: Stephen23
2021 年 1 月 8 日
lat = [45.67, 45.67, 56.89, 78.61]';
lon = [-66.45, -66.45, -65, -67]';
id = [202, 202, 201, 200]';
key = {'A', 'B', 'C', 'C'}';
T = table(lat, lon, id, key)
[C, ia, ic] = unique(T(:, 1:3),'rows');
C.key = splitapply(@(s)join(s,', '),T.key,ic)
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Other Formats についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!