Kirsty White
Kirsty White

Reputation: 1220

count words in cell array matlab

I have a 500x1 cell aray and each row has a certain word in it. How can I count how many occurences of words there is and display it and also display the percentage of each occurence.

For example

The occurence of these words is:

Ans =

     200 Green
     200 Red
     100 Blue

The percentage of these words:

Ans = 

     40% Green
     40% Red
     20% Blue

Upvotes: 4

Views: 5348

Answers (4)

zhao
zhao

Reputation: 11

tricky way without using explicit fors..

clc
close all
clear all

Paragraph=lower(fileread('Temp1.txt'));

AlphabetFlag=Paragraph>=97 & Paragraph<=122;  % finding alphabets

DelimFlag=find(AlphabetFlag==0); % considering non-alphabets delimiters
WordLength=[DelimFlag(1), diff(DelimFlag)];
Paragraph(DelimFlag)=[]; % setting delimiters to white space
Words=mat2cell(Paragraph, 1, WordLength-1); % cut the paragraph into words

[SortWords, Ia, Ic]=unique(Words);  %finding unique words and their subscript

Bincounts = histc(Ic,1:size(Ia, 1));%finding their occurence
[SortBincounts, IndBincounts]=sort(Bincounts, 'descend');% finding their frequency

FreqWords=SortWords(IndBincounts); % sorting words according to their frequency
FreqWords(1)=[];SortBincounts(1)=[]; % dealing with remaining white space

Freq=SortBincounts/sum(SortBincounts)*100; % frequency percentage

%% plot
NMostCommon=20;
disp(Freq(1:NMostCommon))
pie([Freq(1:NMostCommon); 100-sum(Freq(1:NMostCommon))], [FreqWords(1:NMostCommon), {'other words'}]);

Upvotes: 0

Barney Szabolcs
Barney Szabolcs

Reputation: 12524

Here is my solution, should be quite fast.

% example input
example = 'This is an example corpus. Is is a verb?';
words = regexp(example, ' ', 'split');

%your program, result in vocabulary and counts. (input is a cell array called words)
vocabulary = unique(words);
n = length(vocabulary);
counts = zeros(n, 1);
for i=1:n
    counts(i) = sum(strcmpi(words, vocabulary{i}));
end

%process results
[val, idx]=max(counts);
most_frequent_word = vocabulary{idx};

%percentages:
percentages=counts/sum(counts);

Upvotes: 0

Gunther Struyf
Gunther Struyf

Reputation: 11168

First find the unique words in the data:

% set up sample data:
data = [{'red'}; {'green'}; {'blue'}; {'blue'}; {'blue'}; {'red'}; {'red'}; {'green'}; {'red'}; {'blue'}; {'red'}; {'green'}; {'green'}; ]
uniqwords = unique(data);

then find the occurences of this unique words in the data:

[~,uniq_id]=ismember(data,uniqwords);

Then simply count how many times each unique word is found:

uniq_word_num = arrayfun(@(x) sum(uniq_id==x),1:numel(uniqwords));

To get percentages, divide by the sum of total number of data samples:

uniq_word_perc = uniq_word_num/numel(data)

Upvotes: 1

denahiro
denahiro

Reputation: 1199

The idea is that strcmpi compares cell matrices elementwise. This can be used to compare the input names to the unique names in the input. Try the code below.

% generate some input
input={'green','red','green','red','blue'}';

% find the unique elements in the input
uniqueNames=unique(input)';

% use string comparison ignoring the case
occurrences=strcmpi(input(:,ones(1,length(uniqueNames))),uniqueNames(ones(length(input),1),:));

% count the occurences
counts=sum(occurrences,1);

%pretty printing
for i=1:length(counts)
    disp([uniqueNames{i} ': ' num2str(counts(i))])
end

I leave the percentage calculation to you.

Upvotes: 5

Related Questions