Matlab:优化这个?
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您想要的是 WallId 和 WallTime 中唯一数据的摘要,那么您应该
确保 WallId 首先排序。您可以重新组织 WallTime 进行匹配,如下所示:
此外,只有当 WallTime 与 WallId 匹配时,您才会获得正确的答案。
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:
Also, you'll only get the right answer if WallTime matches whenever WallId does.
使用矢量化。
对于ReducedWallTime 也类似。
显式的 for 循环非常慢。使用矢量运算可以大大加快速度。这是优化 MATLAB 代码的一般主题,网络上的各种文档对此进行了详细介绍。
Use vectorization.
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.