- Define the sample data as a cell array.
- Use 'arrayfun' to apply the 'mostcommon' function to each row of the data.
- Output the results using disp.
Finding mode of each row in an array of Strings
5 views (last 30 days)
Show older comments
Currently I have an array with 3 columns and a lot of rows (about 50,000). Each value is a string I essentially want to compare the 3 values in a row and find the most common.
Say my input table looked like the following
Apple Bannana Apple
Cherry Cherry Apple
Mango Mango Mango
My outputs would be
Apple
Cherry
Mango
Please let me know if there is any advice, I have tried mode but it does not work for strings.
0 Comments
Accepted Answer
Naga
on 12 Aug 2024
Dear Manas,
I understand you have a large array with 3 columns and many rows, where each value is a string. You want to find the most common string in each row and output these values. Here’s how you can do in MATLAB.
% Sample data
data = {
'Apple', 'Banana', 'Apple';
'Cherry', 'Cherry', 'Apple';
'Mango', 'Mango', 'Mango'
};
% Apply the function to each row and store results
mostCommonValues = arrayfun(@(i) mostCommon(data(i,:)), 1:size(data, 1), 'UniformOutput', false);
% Display the results
disp(mostCommonValues);
% Function to find the most common element in a cell array row
function commonValue = mostCommon(cellRow)
[uniqueElements, ~, idx] = unique(cellRow);
counts = accumarray(idx, 1);
[~, maxIdx] = max(counts);
commonValue = uniqueElements{maxIdx};
end
This approach should work efficiently even for large datasets like the one you mentioned with 50,000 rows.
Please refer to the below documentation to know more about the function 'arrayfun':
Hope this helps you!
More Answers (2)
Steven Lord
on 14 Aug 2024
If these strings represent data from one of several values in a category, consider storing the data as a categorical array.
str = ["Apple" "Banana" "Apple"; "Cherry" "Cherry" "Apple"; "Mango" "Mango" "Mango"];
C = categorical(str)
What fruits (categories) are present in C?
whichFruits = categories(C)
Can we ask for the most common category in each row?
M = mode(C, 2)
Does this work even if there's a missing value in C?
C(2, 2) = missing
mode(C, 2)
Now in row 2, Apple and Cherry occur equally frequently, but Apple comes first in the list of categories so it's the mode. [Apple (pi) a la mode? ;)]
Can we figure out how many elements of each category are in each row?
[counts, fruit] = histcounts(C(1, :))
or:
counts = countcats(C(1, :)) % No second output, returns counts in categories() order
0 Comments
Voss
on 12 Aug 2024
str = ["Apple" "Banana" "Apple"; "Cherry" "Cherry" "Apple"; "Mango" "Mango" "Mango"]
N = size(str,1);
modes = strings(N,1);
for ii = 1:N
[~,~,idx] = unique(str(ii,:));
modes(ii) = str(ii,mode(idx));
end
disp(modes)
2 Comments
See Also
Categories
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!