How to keep unique rows
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I have cell array matrix:
PreviousTable:
Time Name Grp Rank Index Attribute
2018-01-03 12:34:00 A N 21 2 Good
2018-03-23 20:04:12 V H 21 11 Good
2018-05-17 13:56:00 P B 21 11 Good
CurrentTable:
Time Name Grp Rank Index Attribute
2018-05-17 13:56:00 P B 21 11 Good
2018-04-17 13:56:00 P Q 21 11 Good
2018-06-05 13:56:00 N S 21 11 Good
Based on Name & grp columns, I want to retain the unique rows present in previousTable but not exist in currentTable,
For instance:
2018-01-03 12:34:00 A N 21 2 Good
2018-03-23 20:04:12 V H 21 11 Good
are not exists in currentTable, and I want to retain from the previous Table:
My desired Output:
Time Name Grp Rank Index Attribute
2018-05-17 13:56:00 P B 21 11 Good
2018-04-17 13:56:00 P Q 21 11 Good
2018-06-05 13:56:00 N S 21 11 Good
2018-01-03 12:34:00 A N 21 2 Good
2018-03-23 20:04:12 V H 21 11 Good
0 commentaires
Réponse acceptée
Paolo
le 8 Juin 2018
Modifié(e) : Paolo
le 8 Juin 2018
You can determine the common values between the two cell arrays with intersect. In the code below I combine columns Name and Grp both for both "tables" and compare them. The common values between the two are eliminated from PreviousTable, which is then appended to CurrentTable.
Your two cell array inputs:
PreviousTable:
PreviousTable = {'Time','Name','Grp','Rank','Index','Attribute';
'2018-01-03 12:34:00' , 'A' , 'N' , 21 , 2 , 'Good';
'2018-03-23 20:04:12' , 'V' , 'H' , 21 , 11 ,'Good';
'2018-05-17 13:56:00' , 'P' , 'B' , 21 , 11 ,'Good'};
CurrentTable:
CurrentTable = {'Time','Name','Grp','Rank','Index','Attribute';
'2018-05-17 13:56:00' , 'P' , 'B' , 21 , 11 , 'Good';
'2018-04-17 13:56:00' , 'P' , 'Q' , 21 , 11 , 'Good';
'2018-06-05 13:56:00' , 'N' , 'S' , 21 , 11 , 'Good'};
%Find common values between arrays (C) and corresponding indexes (ia).
[C,ia,~] =
intersect(cell2mat(PreviousTable(2:end,2:3)),cell2mat(CurrentTable(2:end,2:3)),'rows','stable');
%Delete redundant rows from first cell array.
PreviousTable(ia+1,:) = [];
%Append PreviousTable to CurrentTable.
CurrentTable = [CurrentTable;PreviousTable(2:end,:)];
Output of CurrentTable:
{'Time' } {'Name'} {'Grp'} {'Rank'} {'Index'} {'Attribute'}
{'2018-05-17 13:5…'} {'P' } {'B' } {[ 21]} {[ 11]} {'Good' }
{'2018-04-17 13:5…'} {'P' } {'Q' } {[ 21]} {[ 11]} {'Good' }
{'2018-06-05 13:5…'} {'N' } {'S' } {[ 21]} {[ 11]} {'Good' }
{'2018-01-03 12:3…'} {'A' } {'N' } {[ 21]} {[ 2]} {'Good' }
{'2018-03-23 20:0…'} {'V' } {'H' } {[ 21]} {[ 11]} {'Good' }
2 commentaires
Paolo
le 9 Juin 2018
PreviousTable actually has four rows (1 header and 3 rows of data). The +1 is to exclude the first row of PreviousTable since it's a header and does not contain information you want to include in CurrentTable.
Please accept the question since it solved your problem.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!