how to find mode without built-in mode function?
이전 댓글 표시
any suggestions how to write a function that finds a vectors mode WITHOUT using the built-in mode function?
답변 (3개)
the cyclist
2013년 6월 9일
1 개 추천
You could use the hist() function, and use the value that has the highest count.
댓글 수: 5
izabela kudela
2017년 1월 3일
How can I precisely read which value has the highest count?
Walter Roberson
2017년 1월 3일
[counts, centers] = hist(YourVector);
[~, maxidx] = max(counts);
mode_value = centers(maxidx);
Louise Wade
2021년 1월 6일
This gives me non-integer values when all values in my array are integers.
the cyclist
2021년 1월 7일
I think this is because by default, hist() chooses the bin centers by an algorithm that does not guarantee they are in the original vector. Try this instead:
[counts, centers] = histcounts(YourVector,[unique(YourVector) Inf]);
[~, maxidx] = max(counts);
mode_value = centers(maxidx);
the cyclist
2021년 1월 7일
I realized that that code will not find all the modes, if there are multiple. This code should:
[counts, centers] = histcounts(YourVector,[unique(YourVector) Inf]);
max_value = max(counts);
max_idx = (counts==max_value);
mode_values = centers(max_idx)
Walter Roberson
2013년 6월 9일
1 개 추천
unique(), take the third output, put it through accumarray(), find the max()
Roger Stafford
2013년 6월 9일
편집: Roger Stafford
2013년 6월 9일
If you are not allowed to use the 'mode' function, it sounds as though you must use only more primitive functions. Are you allowed to do the following with v as the given column vector?
u = sort(v);
f = find([true;diff(u)~=0;true]);
[~,ix] = max(diff(f));
m = u(f(ix)); % m is most frequent value occurring in v
(Corrected)
댓글 수: 5
dana
2013년 6월 9일
Roger Stafford
2013년 6월 9일
There's nothing magic about it. I'll give an example. Let v be:
v = [9;4;2;4;9;2;4]
Then you get
u = sort(v) = [2;2;4;4;4;9;9]
diff(u) = [0;2;0;0;5;0]
diff(u)~=0 = [F;T;F;F;T;F] Where 'T' & 'F' stand for true & false
[true;diff(u)~=0;true] = [T;F;T;F;F;T;F;T]
f = find([true;diff(u)~=0;true]) = [1;3;6;8]
diff(f) = [2;3;2]
max(diff(f)) is max at second value, hence ix = 2
f(ix) = f(2) = 3
m = u(f(ix)) = u(3) = 4 Therefore the mode value is 4
Does that show you the principles involved?
Louise Wade
2021년 1월 6일
I am trying this and I am getting the below error. Sorry to bring this up on a 7.5 year comment, it's just relevant to something I am doing right now.
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in Question_1 (line 29)
f = find([true;diff(u)~=0;true]);
That depends on the dimensions of v. If it is a column vector this runs without error.
v = [9;4;2;4;9;2;4];
u = sort(v);
[true;diff(u)~=0;true]
v = [9;4;2;4;9;2;4];
v = v.';
u = sort(v);
[true;diff(u)~=0;true]
Louise Wade
2021년 1월 7일
편집: Louise Wade
2021년 1월 7일
I'll try and transpose my row vector into a column vector and try again. If there are multiple values as the mode, would this cause an issue?
EDIT: It seems to be working for me. Thank you so much for helping. All I needed to do was transpose the array.
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!