How to keep unique rows

3 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by