How to find the mode of a string array (no longer works in 2018b)
21 views (last 30 days)
Show older comments
Anil Kamath on 16 Nov 2018
Commented: hp on 1 Apr 2021
I have discovered that the following code worked in Matlab 2018a but now throws an error in 2018b:
It would appear that the new version of matlab only allows computing the mode on numeric arrays. Is this a bug, or a known change to how this function works (I know that in this version, some of these functions now operate on multiple dimensions now)? And is there a quick workaround? So far I've created the following hack to operate on strings, though I expect it's pretty inefficient:
Y = strings(1,size(X,2));
for n = 1:size(Y,2)
C = categorical(X(:,n));
CC = categories(C);
[~,ind] = max(countcats(C));
Adam Danz on 16 Nov 2018
Edited: Adam Danz on 16 Nov 2018
The documentation for both 2018a and 2018b indicate that the first input to mode() "can be a numeric array, categorical array, datetime array, or duration array." One workaround would be to change your string to a categorical vector.
This works in 2018b
If needed, you could convert back to a string:
More Answers (1)
Philip Borghesani on 16 Nov 2018
Use a character vector instead of a string array:
I belive it was a bug that it worked with strings in R2018a. It has always worked with character vectors and sombody was a bit too helpful adding support for strings which can’t simply be treated as a numeric value.
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!