Search code examples
matlabindexingcell-arraymode

multiple indexing and finding mode of cell array


I have an array of integers called indices and a cell array of strings called Categories. How do I find the most frequent string out of only the cells in the array indices?


Solution

  • You can use unique to find the mode of a cell array by finding the mode of the third output and use that to index into the first output of unique. To determine the mode of only the elements specified by indices, you'll want to grab just the subset of elements of Categories indicated by indices and pass those to unique.

    [values, ~, inds] = unique(Categories(indices));
    modeValue = values{mode(inds)};