フィルターのクリア

identify specific unique columns in a cell array

1 回表示 (過去 30 日間)
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 9 月 8 日
Hello, I have the following cell array as an example:
Stations=
'CID' 'C17E' 'C17E' 200 "2016/9/12" "14:46:6" 0
'CID' 'BC54' 'BC54' 200 "2016/9/13" "6:26:2" 0
'SGC' 'BAR2' 'BAR2' 200 "2016/9/13" "8:12:15" 0
'SGC' 'CBARI' 'BAR2' 200 "2016/9/14" "1:58:31" 1
'SGC' 'CBET2' 'BET' 200 "2016/9/14" "1:58:31" 1
'SGC' 'CBUIS' 'CBUIS' 200 "2016/9/14" "1:58:31" 0
'SGC' 'CBET2' 'BET' 200 "2018/7/22" "7:50:30" 1
'CID' 'C17E' 'C17E' 200 "2014/2/2" "14:46:6" 0
'SGC' 'CBUIS' 'CBUIS' 200 "2017/7/1" "1:50:31" 0
I would like to identify unique values considering the first, second, third and fourth column and obtain the following matrix
Reduced_stations=
'CID' 'C17E' 'C17E' 200 "2016/9/12" "14:46:6" 0
'CID' 'BC54' 'BC54' 200 "2016/9/13" "6:26:2" 0
'SGC' 'BAR2' 'BAR2' 200 "2016/9/13" "8:12:15" 0
'SGC' 'CBARI' 'BAR2' 200 "2016/9/14" "1:58:31" 1
'SGC' 'CBET2' 'BET' 200 "2016/9/14" "1:58:31" 1
'SGC' 'CBUIS' 'CBUIS' 200 "2016/9/14" "1:58:31" 0
I would appreciate the help. By the way, the cell array (Stations) does not have the same data type.

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 9 月 8 日
編集済み: Dyuman Joshi 2023 年 9 月 8 日
Here's one approach -
%Input
Stations = {'CID' 'C17E' 'C17E' 200 "2016/9/12" "14:46:6" 0
'CID' 'BC54' 'BC54' 200 "2016/9/13" "6:26:2" 0
'SGC' 'BAR2' 'BAR2' 200 "2016/9/13" "8:12:15" 0
'SGC' 'CBARI' 'BAR2' 200 "2016/9/14" "1:58:31" 1
'SGC' 'CBET2' 'BET' 200 "2016/9/14" "1:58:31" 1
'SGC' 'CBUIS' 'CBUIS' 200 "2016/9/14" "1:58:31" 0
'SGC' 'CBET2' 'BET' 200 "2018/7/22" "7:50:30" 1
'CID' 'C17E' 'C17E' 200 "2014/2/2" "14:46:6" 0
'SGC' 'CBUIS' 'CBUIS' 200 "2017/7/1" "1:50:31" 0};
%Convert to table, as the function unique() can be used for a table
t = cell2table(Stations);
%Columns of interest
col = 1:4;
%Get the indices of the unique combinations of the columns of interest
%in the same order as they occur in the input
[~,idx]=unique(t(:,col),'stable');
%Get the output in terms of table
outtable = t(idx,:)
t = 6×7 table
Stations1 Stations2 Stations3 Stations4 Stations5 Stations6 Stations7 _________ _________ _________ _________ ___________ _________ _________ {'CID'} {'C17E' } {'C17E' } 200 "2016/9/12" "14:46:6" 0 {'CID'} {'BC54' } {'BC54' } 200 "2016/9/13" "6:26:2" 0 {'SGC'} {'BAR2' } {'BAR2' } 200 "2016/9/13" "8:12:15" 0 {'SGC'} {'CBARI'} {'BAR2' } 200 "2016/9/14" "1:58:31" 1 {'SGC'} {'CBET2'} {'BET' } 200 "2016/9/14" "1:58:31" 1 {'SGC'} {'CBUIS'} {'CBUIS'} 200 "2016/9/14" "1:58:31" 0
%or
%Get the output in terms of cell array
outcell = Stations(idx,:)
Stations = 6×7 cell array
{'CID'} {'C17E' } {'C17E' } {[200]} {["2016/9/12"]} {["14:46:6"]} {[0]} {'CID'} {'BC54' } {'BC54' } {[200]} {["2016/9/13"]} {["6:26:2" ]} {[0]} {'SGC'} {'BAR2' } {'BAR2' } {[200]} {["2016/9/13"]} {["8:12:15"]} {[0]} {'SGC'} {'CBARI'} {'BAR2' } {[200]} {["2016/9/14"]} {["1:58:31"]} {[1]} {'SGC'} {'CBET2'} {'BET' } {[200]} {["2016/9/14"]} {["1:58:31"]} {[1]} {'SGC'} {'CBUIS'} {'CBUIS'} {[200]} {["2016/9/14"]} {["1:58:31"]} {[0]}
  1 件のコメント
Jorge Luis Paredes Estacio
Jorge Luis Paredes Estacio 2023 年 9 月 8 日
Thank you very much. It works perfectly.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by