Find unique column values in NxN cell array

1 view (last 30 days)
tybo1mos
tybo1mos on 2 Dec 2022
Answered: Paul on 2 Dec 2022
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 Comments
tybo1mos
tybo1mos on 2 Dec 2022
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 on 2 Dec 2022
Is that result feasible? I thought each table variable has to have the same number of rows?

Sign in to comment.

Answers (1)

Paul
Paul on 2 Dec 2022
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' }

Tags

Community Treasure Hunt

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

Start Hunting!