首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >优化字符串的出现次数

优化字符串的出现次数
EN

Stack Overflow用户
提问于 2012-07-01 03:43:09
回答 2查看 1.1K关注 0票数 1

我必须计算某个字符串被包含在单元数组中的频率。问题是代码太慢了,它几乎需要1秒才能做到这一点。

代码语言:javascript
复制
    uniqueWordsSize = 6; % just a sample number
    wordsCounter = zeros(uniqueWordsSize, 1);
    uniqueWords = unique(words); % words is a cell-array

    for i = 1:uniqueWordsSize
        wordsCounter(i) = sum(strcmp(uniqueWords(i), words));
    end

我目前正在做的是将uniqueWords中的每个单词与单元格数组中的单词进行比较,并使用sum来计算strcmp返回的数组的总和。

我希望有人能帮我优化一下……对于6个单词来说,1秒实在是太多了。

编辑: ismember甚至更慢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-07-01 04:13:50

通过将unique的第三个输出与hist一起使用,可以完全删除该循环

代码语言:javascript
复制
words = {'a','b','c','a','a','c'}
[uniqueWords,~,wordOccurrenceIdx]=unique(words)
nUniqueWords = length(uniqueWords);
counts = hist(wordOccurrenceIdx,1:nUniqueWords)

uniqueWords = 
    'a'    'b'    'c'
wordOccurrenceIdx =
     1     2     3     1     1     3
counts =
     3     1     2
票数 3
EN

Stack Overflow用户

发布于 2014-03-03 17:12:27

不使用显式fors的棘手方法..

代码语言:javascript
复制
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'}]);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11277113

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档