gpt4 book ai didi

mysql - 按一列分组并计算 matlab 中各组的中位数

转载 作者:行者123 更新时间:2023-11-29 21:49:31 27 4
gpt4 key购买 nike

我将下表从 mysql 读取到 matlab 元胞数组中:

Nyse = fetch(conn,'SELECT ticker,date,utcsec,bid,ofr FROM HFE.Quotes where ex="N" order by utcsec,bid;');

Nyse 元胞数组包含 1000000 行。我想计算每秒的中位出价,其中秒被记录为 utcsec 列中的字符串。我通过以下方式做到这一点:

utcsec=cell2mat(Nyse(:,3));
bid=cell2mat(Nyse(:,4));
NyseBid=grpstats(bid,utcsec,{'median'});

问题是 grpstats 函数需要大约 70 秒才能完成任务。问题是,如何优化代码使其运行得更快?

UTCSEC 列中的示例字符串是“09:30:00”。

最佳答案

我建议您结帐this question and these answers因为这是一个高度相关的问题。

要将该线程的结果应用于此问题,我将使用 this MEX function我编写了一个接受 groupID 数组并提取每个组所在行的代码。这允许按组进行有效聚合。

据我了解,utcsec本质上是一个groupID,bid是要聚合的数组。代码如下:

utcsec = Nyse(:,3);    %utcsec in this should be a cell array o fstrings
[unique_utcsec, map] = mg_getRowsWithKey(utcsec); %call to my magic function
%unique_utcsec contains unique strings in utcsec
%map shows us which rows correspond to each unique second

median_bid = zeros(length(unique_utcsec), size(bid,2));

for i = 1:length(unique_utcsec) %iterate over each utc second
median_bid(i,:) = median(bid(map{i},:),1); %calculate the median for that second
end

在我的测试中,这段代码比 Matlab 使用 grpstats 函数的实现要快得多。该线程中还有一些不依赖 mex 的替代方法。 mex c++ 代码应使用以下内容进行编译:

mex -largeArrayDims mg_getRowsWithKey.cpp

然后可以像任何 Matlab 函数一样调用函数 mg_getRowsWithKey。 mg_getRowsWithKey是使用STL库(例如map)用c++编写的。

关于mysql - 按一列分组并计算 matlab 中各组的中位数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33787188/

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