gpt4 book ai didi

performance - 寻找更快的方法来处理细胞和矢量操作

转载 作者:行者123 更新时间:2023-12-02 04:52:33 25 4
gpt4 key购买 nike

我有一个单元格列表,其中每个元素都包含不同数量的坐标以访问矢量。例如,

C ={ [1 2 3] , [4 5],  [6], [1 8 9 12 20]}

这只是一个示例,在实际情况中,C 的大小为 10^4 到 10^6,每个元素包含一个包含 1 到 1000 个元素的向量。我需要使用每个元素作为坐标来访问向量中的相应元素。我正在使用循环来查找由单元格元素指定的向量元素的平均值

 for n=1:size(C,1)
x = mean(X(C{n}));
% put x to somewhere
end

这里的 X 是 10000 个元素的大向量。使用循环是可以的,但我想知道是否有任何方法可以在不使用循环的情况下做同样的事情?我问的原因是上面的代码需要运行很多次,现在使用 lopp 很慢。

最佳答案

方法 #1

C_num = char(C{:})-0; %// 2D numeric array from C with cells of lesser elements 
%// being filled with 32, which is the ascii equivalent of space

mask = C_num==32; %// get mask for the spaces
C_num(mask)=1; %// replace the numbers in those spaces with ones, so that we
%// can index into x witout throwing any out-of-extent error

X_array = X(C_num); %// 2D array obtained after indexing into X with C_num
X_array(mask) = nan; %// set the earlier invalid space indices with nans
x = nanmean(X_array,2); %// final output of mean values neglecting the nans

方法 #2

lens = cellfun('length',C); %// Lengths of each cell in C
maxlens = max(lens); %// max of those lengths

%// Create a mask array with no. of rows as maxlens and columns as no. of cells.
%// In each column, we would put numbers from each cell starting from top until
%// the number of elements in that cell. The ones(true) in this mask would be the
%// ones where those numbers are to be put and zeros(false) otherwise.
mask = bsxfun(@le,[1:maxlens]',lens) ; %//'

C_num = ones(maxlens,numel(lens)); %// An array where the numbers from C are to be put

C_num(mask) = [C{:}]; %// Put those numbers from C in C_num.
%// NOTE: For performance you can also try out: double(sprintf('%s',C{:}))
X_array = X(C_num); %// Get the corresponding X elements
X_array(mask==0) = nan; %// Set the invalid locations to be NaNs
x = nanmean(X_array); %// Get the desired output of mean values for each cell

方法 #3

这与方法 #2 几乎相同,但在末尾进行了一些更改以避免 nanmean

因此,将方法 #2 的最后两行编辑为这些 -

X_array(mask1==0) = 0;
x = sum(X_array)./lens;

关于performance - 寻找更快的方法来处理细胞和矢量操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26646283/

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