- 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
조회 수: 35 (최근 30일)
이전 댓글 표시
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
채택된 답변
Naga
2024년 8월 12일 17:01
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!
추가 답변 (2개)
Steven Lord
2024년 8월 14일 18:23
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
Voss
2024년 8월 12일 16:57
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!