0

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.
1
  • You give examples that are all the same length, 5 characters long. Does the actual data contain the same length strings as well? Commented Oct 30, 2016 at 23:47

1 Answer 1

2

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{:}])
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, thanks, I was experimenting with A{:} and couldn't figure out how to collect the output. It seems like [A{:}] does the same thing. Is this just shorthand for cat(2,A{:})?
@jonem Yes that is a less-expilcit version of the same thing [] performs implicit horizontal concatenation. horzcat(A{:}) also does the same thing, cat performs concatenation on the dimension specified by the first input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.