How do I find the mode of an array and verify it using the frequency?
35 views (last 30 days)
Show older comments
A very basic question, but I am just getting back into it and I'm struggling to find the right answer on the forums (or I very bad at using Google?).
I have an array which is 100x50 and want to find the mode of each row. When I run the below function, it provides a 98% correct solution. Two of the rows have a mix of values and it is not as easy to tell the mode, whereas the other 98 rows have a distint value. Is it possible to set a threshold so that out of the row, if the value is only less than 30% of the total value then ignore it?
So if row 99 has a value of 1 as the mode, but 1 only occurs 20% (10/50) of the time in the total row then ignore it.
mode(a(:,:));
0 Comments
Answers (2)
Walter Roberson
on 25 Aug 2025 at 11:10
By definition, mode() returns a value such that no other value is more frequent (but other values might be equally frequent.)
If a potential candidate for the mode is not one of the values that is the most frequent, then that candidate will not be selected.
Now consider the possibilities: if "1" is the selected mode() for a row, then every other value in the row is either less frequent or the same frequency as "1". If "1" occurs only 20% of the time and if no other value occurs with equal frequency... then every other value must also be less than 20% of the values. If "1" occurs only 20% of the time and some other value occurs with the same frequency, then those other values must also occur less than 20% of the values.
Thus, your condition of ignoring the candidate mode if it occurs less than some percentage of the time, is equivalent to saying that for that particular row, the returned mode value must be either empty or NaN, since every other value on the row must occur with frequency no greater than the given percentage, However, mode() of an array row-wise cannot return empty in any row slot, so mode() of the array would have to return NaN for that row.
Is that what you want? That you get back NaN for rows with low frequency ??
1 Comment
Walter Roberson
on 25 Aug 2025 at 11:14
Umar
on 25 Aug 2025 at 14:54
Edited: Torsten
on 25 Aug 2025 at 17:19
Hi @ Caleb Crawdad,
I reviewed your question regarding row-wise mode computation and the 30% dominance threshold. As you noticed, MATLAB’s built-in `mode` function returns the most frequent value but does not consider whether that value is statistically dominant in the row.
Inspired by Walter Roberson’s vectorized approach on MATLAB Answers, I implemented a _row-wise solution_ that is fully transparent and easy to follow.
*This script:*
* Computes the **row-wise mode** explicitly for each row.
* Calculates the **relative frequency** of the mode.
* Applies a **30% threshold**, marking rows with ambiguous modes as `NaN`.
* Handles **tie-breaks deterministically** (chooses the smallest value).
* Provides full transparency for analysis, showing both the candidate mode and its relative frequency.
*Here’s the MATLAB script:*
clear all; clc; close all;
A = randi([1 5],100,50); % Example 100x50 matrix
threshold = 0.3; % minimum fraction for mode
[rowCount, colCount] = size(A);
RowIndex = (1:rowCount)';
CandidateMode = NaN(rowCount,1);
RelFrequency = NaN(rowCount,1);
ModeWithThreshold = NaN(rowCount,1);
for i = 1:rowCount
row = A(i,:);
[uniqueVals, ~, idx] = unique(row);
counts = accumarray(idx,1);
maxCount = max(counts);
posTies = find(counts == maxCount);
modeVal = min(uniqueVals(posTies));
relFreq = maxCount / colCount;
CandidateMode(i) = modeVal;
RelFrequency(i) = relFreq;
if relFreq >= threshold
ModeWithThreshold(i) = modeVal;
else
ModeWithThreshold(i) = NaN;
end
end
ResultTable = table(RowIndex, CandidateMode, RelFrequency, ModeWithThreshold, ...
'VariableNames', {'RowIndex','CandidateMode','RelFrequency','ModeWithThreshold'});
disp(ResultTable);
*Please see attached*
<</matlabcentral/answers/uploaded_files/1839255/IMG_4536.jpeg>>
*How this addresses your concerns:*
1. Row-wise mode:Computed explicitly per row.
2. Threshold enforcement:Modes below 30% are ignored (`NaN`).
3. Ambiguous rows:Easily identifiable with `NaN`.
4. Mixed-value rows:Tie-break handled deterministically.
5. Transparency: `CandidateMode` and `RelFrequency` show exactly how dominant the mode is.
This solution ensures accurate mode detection while clearly filtering non-dominant rows.
4 Comments
Walter Roberson
on 25 Aug 2025 at 20:37
My code is much much shorter.
A = randi([1 5],100,50); % Example 100x50 matrix
threshold = 0.3; % minimum fraction for mode
[M, F] = mode(A, 2);
M1 = M;
M1(F < size(A,2)*threshold) = NaN;
table((1:size(M,1)).', M, F./size(A,2), M1)
[rowCount, colCount] = size(A);
RowIndex = (1:rowCount)';
CandidateMode = NaN(rowCount,1);
RelFrequency = NaN(rowCount,1);
ModeWithThreshold = NaN(rowCount,1);
for i = 1:rowCount
row = A(i,:);
[uniqueVals, ~, idx] = unique(row);
counts = accumarray(idx,1);
maxCount = max(counts);
posTies = find(counts == maxCount);
modeVal = min(uniqueVals(posTies));
relFreq = maxCount / colCount;
CandidateMode(i) = modeVal;
RelFrequency(i) = relFreq;
if relFreq >= threshold
ModeWithThreshold(i) = modeVal;
else
ModeWithThreshold(i) = NaN;
end
end
ResultTable = table(RowIndex, CandidateMode, RelFrequency, ModeWithThreshold, ...
'VariableNames', {'RowIndex','CandidateMode','RelFrequency','ModeWithThreshold'})
Umar
on 28 Aug 2025 at 23:42
Absolutely right, @Walter Robertson! That's a beautifully elegant solution - much more concise and leverages MATLAB's built-in functions perfectly.Your approach using mode(A, 2) directly is brilliant:
- Gets both the mode values AND frequencies in one function call
- The threshold filtering with M1(F < size(A,2)*threshold) = NaN is clean and vectorized
- No loops needed at all!
This is exactly why I appreciate working with experienced MATLAB developers - there's always a more elegant built-in solution than the verbose manual approach. Your code is not just shorter, it's also more readable and computationally efficient.
Thanks for sharing this - definitely learning from your approach here. Sometimes the best solutions are the simplest ones that fully utilize MATLAB's strengths!
See Also
Categories
Find more on Matrices and Arrays 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!