Find unique column values in NxN cell array

2 次查看(过去 30 天)
I have a 1000x50 cell array with each column contining multiple duplicate entries.
simplified example:
names = {'red','blue','green';'red','orange','yellow';'orange','orange','red'};
{'red' } {'blue' } {'green' }
{'red' } {'orange'} {'yellow'}
{'orange'} {'orange'} {'red' }
I would to create a simplified array wich contains only the unique values from each of the indiviual columns:
result:
{'red' } {'blue' } {'green' }
{'orange'} {'orange'} {'yellow'}
{'red' }
  2 个评论
tybo1mos
tybo1mos 2022-12-2
My original data is actually in table form. So my prference would be to work from the original table data, and rutun a table with only unique values.
Starting:
color_1 color_2 color_3
________ ________ ________
'red' 'blue' 'green'
'red' 'orange' 'yellow'
'orange' 'orange' 'red'
Reult
color_1 color_2 color_3
_______ ________ ________
'red' 'blue' 'green'
'orange' 'orange' 'yellow'
'red'
Paul
Paul 2022-12-2
Is that result feasible? I thought each table variable has to have the same number of rows?

请先登录,再进行评论。

回答(1 个)

Paul
Paul 2022-12-2
Because the resulting variables could all have a different number of elements, I'm converting each variable into a scalar cell that contains the desired data
t = table({'red';'red';'orange'},{'blue';'orange';'orange'},{'green';'yellow';'red'})
t = 3×3 table
Var1 Var2 Var3 __________ __________ __________ {'red' } {'blue' } {'green' } {'red' } {'orange'} {'yellow'} {'orange'} {'orange'} {'red' }
t = varfun(@(x){unique(x,'stable')},t)
t = 1×3 table
Fun_Var1 Fun_Var2 Fun_Var3 __________ __________ __________ {2×1 cell} {2×1 cell} {3×1 cell}
t.(1){:}
ans = 2×1 cell array
{'red' } {'orange'}
t.(2){:}
ans = 2×1 cell array
{'blue' } {'orange'}
t.(3){:}
ans = 3×1 cell array
{'green' } {'yellow'} {'red' }

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by