Optimization Matlab:对此进行优化(第2部分)

Optimization Matlab:对此进行优化(第2部分),optimization,matlab,vectorization,Optimization,Matlab,Vectorization,这里还有一个: ValidFirings = ((DwellTimes > 30/(24*60*60)) | (GroupCount > 1)); for i = length(ValidFirings):-1:2 if(~ValidFirings(i)) DwellTimes(i-1) = DwellTimes(i)+DwellTimes(i-1); GroupCount(i-1) = GroupCount(i)+GroupCount(i-

这里还有一个:

ValidFirings = ((DwellTimes > 30/(24*60*60)) | (GroupCount > 1));

for i = length(ValidFirings):-1:2
    if(~ValidFirings(i))
        DwellTimes(i-1) = DwellTimes(i)+DwellTimes(i-1);
        GroupCount(i-1) = GroupCount(i)+GroupCount(i-1);
        DwellTimes(i) = [];
        GroupCount(i) = [];
        ReducedWallTime(i) = [];
        ReducedWallId(i) = [];
    end
end
其目的似乎是根据传感器点火是否有效来总结“停留时间”。因此,我有一个传感器触发向量,我向后走,如果当前行没有标记为有效,则求和到前一行

我可以用C/C++将其可视化,但我不知道如何将其转换为更好的Matlab向量表示法。现在,这个循环是v慢的

编辑:
我是否可以使用某种形式的常驻时间=常驻时间(总和(有效字符串))?

我将首先如图所示合并,然后消除无效数据。这样可以避免不断调整数据大小。请注意,由于值的传播方式,您不能反转FOR循环的顺序

ValidFirings = ((DwellTimes > 30/(24*60*60)) | (GroupCount > 1));

for i = length(ValidFirings):-1:2
    if (~ValidFirings(i))
        DwellTimes(i-1) = DwellTimes(i) + DwellTimes(i-1);
        GroupCount(i-1) = GroupCount(i) + GroupCount(i-1);
    end
end

DwellTimes      = DwellTimes(ValidFirings);
GroupCount      = GroupCount(ValidFirings);
ReducedWallTime = ReducedWallTime(ValidFirings);
ReducedWallId   = ReducedWallId(ValidFirings);

与前面的问题一样,替换for循环应该可以提高性能

%# Find the indices for invalid firings
idx = find(~(DwellTimes > 30/(24*60*60)) | (GroupCount > 1));

%# Index the appropriate elements and add them (start the addition
%# from the second element)
%# This eliminates the for loop
DwellTimes(idx(2:end)-1) = DwellTimes(idx(2:end)-1)+DwellTimes(idx(2:end));
GroupCount(idx(2:end)-1) = GroupCount(idx(2:end)-1)+GroupCount(idx(2:end));

%# Now remove all the unwanted elements (this removes the 
%# first element if it was a bad firing.  Modify as necessary)
GroupCount(idx)=[];
DwellTimes(idx)=[];

我得到第一部分:删除“清除内存”部分。我看不出第二部分发生了什么。这看起来像是将ValidFlings向量应用于压缩之前的时间等的值。这些值将不再适用。请查找逻辑订阅。这与
relatetimes=relatetimes(find(validfings)),等等。所以您只保留
validfillings==true
的部分。谢谢!我仍然对Matlab中的矢量化符号感到困惑。每一段代码都有帮助!