Search this site
Embedded Files
Matlab by Examples
  • Tutorial
  • File operations
    • File commands
    • Files: read & write
      • fprintf
  • Matrix
    • cell array
      • Convert cell array
      • Single value to multiple fields
      • str2num
    • Check for subset
    • Create matrix
    • Index pairs permutations
    • Matrix to vector
    • Number range
    • Sort
      • Multiple columns
      • Reverse order of elements
    • triu elements
  • Multiprocessing
  • Plot
    • Axes
    • Bar plot
      • Barplot colors
      • Barplot percentage
    • Clustergram
      • to figure
    • Colors
      • Sample colors
    • Histogram
      • Histogram colors
      • Horizontal histogram
    • Remove top and right axes
    • Save as png
    • Subplot
    • Subtitle
  • Strings
    • Add prefix
    • Frequencies
    • Join
    • remove
  • Structure
  • System
    • Command line
      • Error
  • Time
Matlab by Examples

Frequencies

Text, words & characters (string data structure)

Get frequency table (sorted alphabetically)

s = {'A','E','C','B','E','B','E','A'}

tabulate(sort(s))

    Value    Count   Percent

      A        2     25.00%

      B        2     25.00%

      C        1     12.50%

      E        3     37.50%


Get frequency table (sorted in descending order of percentages)

s  = {'A','E','C','B','E','B','E','A'}

t  = tabulate(s)

ts = sortrows(t,3,'descend')

    {'E'}    {[3]}    {[37.5000]}

    {'A'}    {[2]}    {[     25]}

    {'B'}    {[2]}    {[     25]}

    {'C'}    {[1]}    {[12.5000]}

for i=1:size(ts,1)

  fprintf(1, ' %s\t%d\t%.2f%%\n', ts{i,:})

end

  E 3 37.50%

  A 2 25.00%

  B 2 25.00%

  C 1 12.50%


https://mathworks.com/help/stats/tabulate.html

www.nlpca.org

Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse