find unique words in a 54x1 cell array
Show older comments
I have a cell array of the states used in a data collection. Some of the states are used more than once. I need to find how mant unique states were used in the collection. I have tried to use unique but the I can't seem to get it to work due to the cells being letters not numerical characters.
Answers (1)
states = {'red', 'yellow', 'green', 'blue', 'cyan', 'blue', 'yellow', 'green', 'orange'}
unique(states)
length(ans)
6 Comments
Michael Grubb
on 2 Apr 2022
{'red', 'yellow'} and so on is not a string, it is a cell array of character vectors. You can do the same thing with string objects, though.
states = string({'red', 'yellow', 'green', 'blue', 'cyan', 'blue', 'yellow', 'green', 'orange'})
unique(states)
length(ans)
Walter Roberson
on 2 Apr 2022
Is it possible that you have a cell array of numeric scalar values? If so then use cell2mat() to convert into a numeric array that you can then unique()
Michael Grubb
on 3 Apr 2022
Michael Grubb
on 3 Apr 2022
["AL"; "AZ";...etc... "WY"]
That is not a cell array, that is a string() array, and unique() would work fine on it.
Is it possible that you have a cell array, and inside the cell array you have scalar string objects?
states = {"red", "yellow", "green", "blue", "cyan", "blue", "yellow", "green", "orange"}
unique(vertcat(states{:}))
length(ans)
Categories
Find more on Characters and Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!