How to join matching rows of cells in a particular column order

3 ビュー (過去 30 日間)
Marcus Glover
Marcus Glover 2019 年 11 月 20 日
コメント済み: Marcus Glover 2019 年 11 月 20 日
I have a cell array containtin strings and numbers. I'd like to combine rows that have matching key values, but I need the colums to stay in the same place. Most rows will have a single matching row- more than one match is not possible. Some cells in every row will have empty (0x0 double) values- these essentially will be overwritten by the data in the matching row column.
a={'201901' 'timeA' 123 []; ...
'201901' 'timeB' [] 22; ...
'201901' 'timeA' [] 456; ...
'201902' 'timeA' 999 []; ...
'201901' 'timeB' 11 []}
the output should be:
b={'201901' 'timeA' 123 456;...
'201901' 'timeB' 11 22; ...
'201902' 'timeA' 999 []}
Not sure if there is something like join for cells. Seem ls like unique(a(:,1:2),'rows') will not work on cells either.

採用された回答

Matt J
Matt J 2019 年 11 月 20 日
編集済み: Matt J 2019 年 11 月 20 日
Not sure why you're working with cells, rather than tables, but it's easy enough to switch back and forth.
A=cell2table(a);
B=varfun(@(z){max([z{:}])}, A,'GroupingVariables',[1,2]); %EDITED
B.GroupCount=[];
b=table2cell(B)
  3 件のコメント
Matt J
Matt J 2019 年 11 月 20 日
編集済み: Matt J 2019 年 11 月 20 日
Here's what I get,
>> A=cell2table(a);
B=varfun(@(z){max([z{:}])}, A,'GroupingVariables',[1,2]);
B.GroupCount=[];
b=table2cell(B)
b =
3×4 cell array
{'201901'} {'timeA'} {[123]} {[ 456]}
{'201901'} {'timeB'} {[ 11]} {[ 22]}
{'201902'} {'timeA'} {[999]} {0×0 double}
Maybe you were using a previous edit ...
Marcus Glover
Marcus Glover 2019 年 11 月 20 日
Thanks, works now.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMonte Carlo Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by