How to find the mode of a string array (no longer works in 2018b)

조회 수: 20 (최근 30일)
Anil Kamath
Anil Kamath 2018년 11월 16일
댓글: hp 2021년 4월 1일
I have discovered that the following code worked in Matlab 2018a but now throws an error in 2018b:
mode(["A","A","B"],2)
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));
if ~isempty(ind)
Y(n)=string(CC(ind));
end
end

채택된 답변

Adam Danz
Adam Danz 2018년 11월 16일
편집: Adam Danz 2018년 11월 16일
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
mode(categorical( ["A","A","B"]))
ans =
categorical
A
If needed, you could convert back to a string:
string(mode(categorical( ["A","A","B"])))
  댓글 수: 2
Anil Kamath
Anil Kamath 2018년 11월 19일
Thanks!
It's a shame this doesn't work using the native mode function on strings, as it means you can't quickly compute the mode in a 2D array along a single dimension without converting each slice into a categorical array one by one, and computing the mode of each categorical slice. It feels like a very natural interpretation for the mode of a string to be the string which occurs most frequently...

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Philip Borghesani
Philip Borghesani 2018년 11월 16일
Use a character vector instead of a string array:
mode(['A','A','B'],2)
% or
mode('AABBCCC',2)
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.
  댓글 수: 1
Anil Kamath
Anil Kamath 2018년 11월 19일
I'm afraid this won't work in my use-case as it has to work on multi-character strings rather than just scalar chars (I put a simplified example in my initial question). Adam's answer above looks like it works.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by