plot the distribution of letters in common words in matlab

To plot the distribution of letters in common words in MATLAB, you can follow these general steps:

  1. Choose a set of common words that you want to analyze. You can use MATLAB's webread function to fetch a list of common words from a website or load a predefined list from a file.

  2. For each word, compute the frequency of occurrence of each letter. You can use the histcounts function to count the number of occurrences of each letter in a word.

  3. Compute the relative frequency of each letter, i.e., the number of times a letter appears divided by the total number of letters in all the words.

  4. Plot the relative frequency of each letter using a histogram. You can use the histogram function in MATLAB to plot a histogram.

Here's an example code snippet that demonstrates how to do this for a list of common words:

main.m
% Load a list of common words
words = {'the', 'and', 'that', 'but', 'for', 'with', 'are', 'was', 'his', 'one'};

% Compute the frequency of each letter in all the words
letters = 'abcdefghijklmnopqrstuvwxyz';
counts = zeros(size(letters));
for i = 1:numel(words)
    word = lower(words{i});
    [~,~,idx] = intersect(word, letters);
    counts(idx) = counts(idx) + histcounts(idx, numel(word)+1);
end

% Compute the relative frequency of each letter
freqs = counts / sum(counts);

% Plot the histogram of relative frequencies
figure;
bar(freqs);
xlabel('Letter');
ylabel('Relative frequency');
594 chars
21 lines

This code generates a histogram that shows the distribution of letters in common English words.

gistlibby LogSnag