How to keep unique rows

5 visualizaciones (últimos 30 días)
Mekala balaji
Mekala balaji el 8 de Jun. de 2018
Comentada: Paolo el 9 de Jun. de 2018
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

Respuesta aceptada

Paolo
Paolo el 8 de Jun. de 2018
Editada: Paolo el 8 de Jun. de 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 comentarios
Mekala balaji
Mekala balaji el 9 de Jun. de 2018
It works,
But Sir,
ia+1 is 4, and PreviousTable has only three rows, kindly explain the logic,
Paolo
Paolo el 9 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by