Matlab:优化此(第 2 部分)

发布于 2024-12-07 13:47:18 字数 643 浏览 1 评论 0原文

这是另一个:

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 向量表示法。就目前情况而言,这个循环非常慢。

编辑: 我可以使用某种形式的 DwellTimes = DwellTimes( cumsum( ValidFirings )) 吗?

Here's another one:

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

It appears that the intent is to sum up 'dwelltimes' based on whether or not the sensor firing is considered valid. So I have a vector of sensor firings that Im walking through backwards and summing into the previous row if the current row is not marked as valid.

I can visualize this in C/C++ but I don't know how to translate it into better Matlab vector notation. As it stands now, this loop is v slow.

EDIT:
Could I use some form of DwellTimes = DwellTimes( cumsum( ValidFirings ))?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心清如水 2024-12-14 13:47:18

与您之前的问题一样,替换 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)=[];

As with your previous question, replacing the for loop should improve the performance.

%# 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)=[];
不再让梦枯萎 2024-12-14 13:47:18

我会先如图所示进行合并,然后消除无效数据。这避免了不断调整数据大小。请注意,由于值传播的方式,您无法颠倒 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);

I would consolidate first as shown, then eliminate the invalid data. This avoids the constant resizing of the data. Note that you can't reverse the order of the FOR loop due to the way that the values propagate.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文