在MATLAB中,find()函数有更快的替代方法吗



我正在运行一个动力学蒙特卡罗模拟代码,其中我有一个大型稀疏数组,我首先计算它的cumsum((,然后使用find((找到大于或等于给定值的第一个元素。

vecIndex = find(cumsum(R) >= threshold, 1);

由于我调用该函数的次数很多,所以我想加快代码的速度。有没有更快的方法来执行此操作?

完整的功能:

function Tr = select_transition(Fr,Rt,R)
N_dep = (1/(Rt+1))*Fr; %N flux-rate
Ga_dep = (1-(1/(Rt+1)))*Fr; %Ga flux-rate
Tr = zeros(4,1);
RVec = R(:, :, :, 3);
RVec = RVec(:);
sumR = Fr + sum(RVec); %Sum of the rates of all possible transitions
format long
sumRx = rand * sumR; %for randomly selecting one to the transitions 
%disp(sumRx);
if sumRx <= Fr %adatom addition
Tr(1) = 0;
if sumRx <= Ga_dep
Tr(2) = 10; %Ga deposition
elseif sumRx > Ga_dep
Tr (2) = -10; %N deposition
end
else
Tr(1) = 1; %adatom hopping
vecIndex = find(cumsum(RVec) >= sumRx - Fr, 1);
[Tr(2), Tr(3), Tr(4)] = ind2sub(size(R(:, :, :, 3)), vecIndex); %determines specific hopping transition 
end
end

如果Rvec是稀疏的,则提取其非零值和相应的索引并将cumsum应用于这些值会更有效。

Tr(1) = 1;
[r,c,v] = find(RVec); % extract nonzeros
cum = cumsum(v);
f = find(cum >= sumRx - Fr, 1);
Tr(2) = r(f);
sz = size(R);
[Tr(3), Tr(4)] = ind2sub(sz(2:3), c(f));

相关内容

  • 没有找到相关文章