How to keep unique rows

5 次查看(过去 30 天)
Mekala balaji
Mekala balaji 2018-6-8
评论: Paolo 2018-6-9
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

采纳的回答

Paolo
Paolo 2018-6-8
编辑:Paolo 2018-6-8
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 个评论
Mekala balaji
Mekala balaji 2018-6-9
It works,
But Sir,
ia+1 is 4, and PreviousTable has only three rows, kindly explain the logic,
Paolo
Paolo 2018-6-9
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by