How to conditionally merge rows in a table

12 vues (au cours des 30 derniers jours)
Blue
Blue le 8 Jan 2021
Modifié(e) : Stephen23 le 8 Jan 2021
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,

Réponse acceptée

Stephen23
Stephen23 le 8 Jan 2021
Modifié(e) : Stephen23 le 8 Jan 2021
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)
T = 4x4 table
lat lon id key _____ ______ ___ _____ 45.67 -66.45 202 {'A'} 45.67 -66.45 202 {'B'} 56.89 -65 201 {'C'} 78.61 -67 200 {'C'}
[C, ia, ic] = unique(T(:, 1:3),'rows');
C.key = splitapply(@(s)join(s,', '),T.key,ic)
C = 3x4 table
lat lon id key _____ ______ ___ ________ 45.67 -66.45 202 {'A, B'} 56.89 -65 201 {'C' } 78.61 -67 200 {'C' }

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by