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));
unique(arr, 'rows').arr in the question is equivalent to ['helloworld','helloagain';,'I----said-';'helloagain']arr wrong.It is better to use cell arrays and regexp function; the behavior of string arrays may not be what you expect.
arr = {'hello', 'world'; 'hello', 'again'; 'I----', 'said-'; 'hello', 'again'};
If you use
hellos = sum(~cellfun('isempty', regexp(arr, 'hello')));
it will return the number of 'hello''s in cell array arr.