2012-07-19 7 views
4

나는 500x1 셀 레이를 가지고 있으며 각 행에는 특정 단어가 있습니다. 얼마나 많은 단어가 있는지를 계산하여 표시하고 각 발생의 비율을 표시하는 방법은 무엇입니까? 예를셀 배열 matlab에 단어 개수

이 단어의 선두로부터 들어

은 다음과 같습니다

Ans = 

    200 Green 
    200 Red 
    100 Blue 

이 단어의 비율 :

Ans = 

    40% Green 
    40% Red 
    20% Blue 
+0

이미 고유의 단어가 원본에있는 목록이 있나요 세포 배열을 500x1? –

+0

사실, 나는 방금 문제를 다루는 멋진 솔루션을 찾았습니다. [Answer of @Peter] (http://stackoverflow.com/a/13593029/1705967) –

답변

5

아이디어는 strcmpi 셀 매트릭스 elementwise를 비교합니다. 이것은 입력 이름을 입력의 고유 이름과 비교하는 데 사용할 수 있습니다. 아래 코드를 사용해보십시오.

% 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 

저는 계산 해보겠습니다.

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

는 다음 데이터에 독특한 단어의 발행 수를 찾을 : 데이터의 고유 단어를 찾을

+0

하나의 단일 사례로 발생 행을 변경합니다. 낮거나 위. 먼저이 작업을 수행합니다. input = lower (입력); 모든 문자열을 소문자로 반환합니다. 훨씬 더 쉽게 크기가 일치하지 않는 경우 발생할 수 있습니다 .. 그냥 임의의 의견 – user2867655

1

먼저

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

그리고 단순히 각각의 고유 한 단어가 발견 얼마나 많은 시간을 계산 :

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

데이터 샘플들의 총 수의 합으로 나누어 비율을 효율적으로 활용하려면 :

uniq_word_perc = uniq_word_num/numel(data) 
+0

군터 어떻게 denahiro의 답변에 대한 비율을 계산할 것이라고? –

+0

여기서와 같은 방식으로 결과 카운트를 총 샘플 수로 나눕니다. –

0

여기 내 해결책은 매우 빠릅니다. 명시 적 FORS를 사용하지 않고

% 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); 
0

교묘 한 방법 ..

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'}]);