Increment loop index based on a condition
4 次查看(过去 30 天)
显示 更早的评论
I have an array that has 100000+ values. every 3000th sample to a new array based on a condition.
1. I should compare every 3000th value and see if it differs from the previous 3000th sample by greater than 10. If so, I should add the sample, sample+100th element, sample+200th element to my new array.
2. If not, I should only add the 3000th sample element to my new array and then move to the next 3000th sample.
I have implemented this as an if-else loop inside a for loop. My problem is I am unable to increment the loop index if I first enter the if-loop and then go to the else loop next. I have tried using a while loop instead;however, the while loop executes forever and I don't get any output.
Any help is appreciated.
Thanks.
0 个评论
采纳的回答
Walter Roberson
2017-12-26
output = [];
for K = 6000 : 3000 : length(YourVector)
if abs(YourVector(K) - YourVector(K-3000)) > 10
output = [output, YourVector(K+[0 100 200])];
else
output = [output, YourVector(K)];
end
end
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!