gpt4 book ai didi

matlab - 有效地将数据拆分为 bin

转载 作者:太空宇宙 更新时间:2023-11-03 19:50:15 25 4
gpt4 key购买 nike

我想将我的 data 变量拆分为不同的变量 a bc,并应用 mean 到垃圾箱(第一维)。有没有办法在速度方面大幅(例如 1 倍数量级)改进此代码?欢迎一般反馈

data=rand(20,1000); %generate data
bins=[5 10 5]; %given size of bins
start_bins=cumsum([1 bins(1:end-1)]);
end_bins=cumsum([bins]);
%split the data into 3 cell arrays and apply mean in 1st dimension
binned_data=cellfun(@(x,y) mean(data(x:y,:),1),num2cell(start_bins),num2cell(end_bins),'uni',0);
%data (explicitly) has be stored into different variables
[a,b,c]=deal(binned_data{:});
whos a b c
Name Size Bytes Class Attributes

a 1x1000 8000 double
b 1x1000 8000 double
c 1x1000 8000 double

最佳答案

您可以使用 splitapply (accumarray 稍微友善一点的弟弟):

% Your example
data = rand(20,1000); % generate data
bins = [5 10 5]; % given size of bins

% Calculation
bins = repelem(1:numel(bins), bins).'; % Bin sizes to group labels
binned_data = splitapply( @mean, data, bins ); % splitapply for calculation

binned_data 的行是您的abc

关于matlab - 有效地将数据拆分为 bin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54217931/

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