I have a string array, for instance:
arr = ['hello'; 'world'; 'hello'; 'again'; 'I----'; 'said-'; 'hello'; 'again']
How can I extract the most frequent string, which is 'hello'
in this example?
First step, use a cell array rather than string array:
arr = {'hello', 'world'; 'hello', 'again'; 'I----', 'said-'; 'hello', 'again'};
Second, use unique to get the unique strings (this doesn't work on a string array, which is why I suggest the cell):
[unique_strings, ~, string_map]=unique(arr);
Then use mode on the string_map variable to find the most common values:
most_common_string=unique_strings(mode(string_map));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With