Matlab:如果某个条件为真,则从数组中删除行时出现问题
创建数据集数组(数据)后,我想删除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定要循环到
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 justlength(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, :) = [];