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 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