gpt4 book ai didi

matlab - Matlab : fast code? 中 char 单元格中的计数频率

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:29 26 4
gpt4 key购买 nike

我在 Matlab 中有一个 1x2 单元 AA{i} 是维度为 30494866x1 的单元格,对应于 i=1,2A{i}(j)1x21 字符,用于 i=1,2j=1,..., 30494866

例如我在这里报告A{2}(1:3)

'116374117927631468606'
'112188647432305746617'
'116374117927631468606'

我想计算 A{2} 中每个 1x21 字符重复了多少次。例如,只考虑 A{2}(1:3),我想得到

'116374117927631468606'  2
'112188647432305746617' 1

我现在正在做的是

a=unique(A{2},'stable'); 
b=cellfun(@(x) sum(ismember(A{2},x)),a);

然而,这非常慢(从昨天开始运行)。您对我如何加速代码有什么建议吗?

最佳答案

因为您想知道每个 21 个字符的字符串使用了多少次:

1) sort the cell
2) count how many times each string is used in a for loop.

你的代码是 O(n^2) 所以它很慢。这应该不到一分钟。

基于你的代码

B=sort(A{2}); 
U=sort(unique(B));
C=zeros(numel(U),1);
cnt = 1;
for j=1:numel(B)
if strcmp(U(cnt),B(j))==1
C(cnt)=C(cnt)+1;
else
cnt = cnt +1;
if cnt <= numel(U)
C(cnt) = C(cnt)+1;
end
end
end

关于matlab - Matlab : fast code? 中 char 单元格中的计数频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42272464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com