use*_*193 5 performance matlab
我想将我的data
变量拆分为不同的变量a
b
和c
,并应用于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
Run Code Online (Sandbox Code Playgroud)
你可以使用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
Run Code Online (Sandbox Code Playgroud)
的行binned_data
是您的a
,b
和c
。