jonem
jonem

Reputation: 459

Finding unique elements in a nested cell array of strings

Define

A = {{'str_a','str_b'},{'str_b','str_c'},{'str_a'},{'str_b','str_c','str_d'}}

I want to find the unique elements of the above. That is, I want the output to be

{'str_a','str_b','str_c','str_d'}

Applying unique results in the following error

unique(A)
Error using cell/unique (line 85)
Input A must be a cell array of strings.

Upvotes: 0

Views: 341

Answers (1)

Suever
Suever

Reputation: 65430

You can flatten the nested cell arrays into a single cell array using {:} indexing combined with horizontal concatenation, and then use unique on that to find the unique strings.

unique(cat(2, A{:}))    % Or unique([A{:}])

Upvotes: 2

Related Questions