Matlab:如果某个条件为真,则从数组中删除行时出现问题

发布于 2024-11-04 22:24:48 字数 302 浏览 1 评论 0原文

创建数据集数组(数据)后,我想删除 Var4 取特定值的所有行。这是我到目前为止所做的:

for i=1:length(data.perf)
    if data.Var4(i)==2
        data(i,:)=[]
    end
end

当然,问题是在条件成立的每次运行中数组都会变短,因此它会在检查所有行之前停止。当 i=length(data.perf) 时,数组会短约 50 行。我想你们已经明白这个问题了。有人可以建议我一个优雅的解决方案吗?以后我一定会经常做这样的事情。

After creating a dataset array (data), I want to delete all rows for which Var4 takes a certain value. Here is what I've done so far:

for i=1:length(data.perf)
    if data.Var4(i)==2
        data(i,:)=[]
    end
end

The problem of course is that the array gets shorter in every run the condition holds true, so that it stops before all lines are checked. When i=length(data.perf) the array is some 50 lines shorter. I think you guys get the problem. Can somebody please suggest me an elegant solution? I will have to do things like this quite often in the future.

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

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

发布评论

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

评论(1

秋心╮凉 2024-11-11 22:24:48

您确定要循环到 length(data.perf) 而不仅仅是 length(data) 吗?从上下文中不清楚,但更有意义...

第一个建议:反转循环可以解决数组变短的问题(for i = length(data.perf):-1:1...)

更优雅的解决方案是不使用 for 循环

data(data.Var4==2, :) = [];

Are you sure you want to loop to length(data.perf) and not just length(data)? It's not clear from the context but would make more sense...

First suggestion: Reversing your loop could solve the problem of the array getting shorter (for i = length(data.perf):-1:1...)

The more elegant solution would be to do it without a for loop

data(data.Var4==2, :) = [];

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