Matlab:优化这个?

发布于 2024-12-07 03:33:05 字数 427 浏览 1 评论 0原文

我是 matlab 新手。已承担加快程序速度的任务。我确信有更好的方法来执行以下语句:

for i = 2:length(WallId)
     if WallId(i) ~= WallId(i-1)
        ReducedWallId = [ReducedWallId;WallId(i)];
        ReducedWallTime = [ReducedWallTime;WallTime(i)];
        GroupCount = [GroupCount;tempCount];
        tempCount = 1;
    else
        tempCount = tempCount +1;
    end
end

我可以将各种变量预先分配给“length(WallId)”,但是完成后我该如何处理额外的变量呢?我在乎吗?

Im v new to matlab. Have been tasked with speeding up a procedure. Im sure there is a better way to do the following statements:

for i = 2:length(WallId)
     if WallId(i) ~= WallId(i-1)
        ReducedWallId = [ReducedWallId;WallId(i)];
        ReducedWallTime = [ReducedWallTime;WallTime(i)];
        GroupCount = [GroupCount;tempCount];
        tempCount = 1;
    else
        tempCount = tempCount +1;
    end
end

I can preallocate the various vars to 'length(WallId)' but what do I do with the extra after its done? Do I care?

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

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

发布评论

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

评论(2

亚希 2024-12-14 03:33:05
idx = find([true diff(WallId) ~= 0]);
ReducedWallId = WallId(idx);
ReducedWallTime = WallTime(idx);
GroupCount = diff([idx numel(WallId)+1]);

假设您想要的是 WallId 和 WallTime 中唯一数据的摘要,那么您应该
确保 WallId 首先排序。您可以重新组织 WallTime 进行匹配,如下所示:

[WallId, ind] = sort(WallId);
WallTime = WallTime(ind);

此外,只有当 WallTime 与 WallId 匹配时,您才会获得正确的答案。

idx = find([true diff(WallId) ~= 0]);
ReducedWallId = WallId(idx);
ReducedWallTime = WallTime(idx);
GroupCount = diff([idx numel(WallId)+1]);

Assuming what you want is a summary of the unique data in WallId and WallTime, then you should
make sure that WallId is sorted first. You can re-organise WallTime to match, as follows:

[WallId, ind] = sort(WallId);
WallTime = WallTime(ind);

Also, you'll only get the right answer if WallTime matches whenever WallId does.

我不咬妳我踢妳 2024-12-14 03:33:05

使用矢量化。

ReducedWallId=WallId(find(diff(WallId)~=0));

对于ReducedWallTime 也类似。

显式的 for 循环非常慢。使用矢量运算可以大大加快速度。这是优化 MATLAB 代码的一般主题,网络上的各种文档对此进行了详细介绍。

Use vectorization.

ReducedWallId=WallId(find(diff(WallId)~=0));

and similarly for ReducedWallTime.

The explicit for-loops are extremely slow. Using vector-operations speeds everything up considerably. This is a general theme in optimizing MATLAB code and is covered in detail in various documents found in the web.

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