Array concatenation based on condition.
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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, sample+300, sample+400 sample+500th elements 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.
Now I have an added condition to this problem : 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, sample+300th element, sample+400th element, sample+500th element to my new array.
If one of the (sample+100th element, sample+200th element, sample+300th element, sample+400th element, sample+500th element) in the same order are different from any of the previous samples by 10, then I have to add the next 5 samples to the array. For eg : if (sample+300)-(sample+100)>10, then the new array becomes [output, sample+100, sample+200, sample+300,sample+400,sample+500,sample+600,sample+700,sample+800];
eg2 : if ((sample+500)-(sample+200)>10), then the new array becomes [output, sample+100, sample+200, sample+300, sample+400,sample+500,sample+600,sample+700,sample+800,sample+900,sample+1000];
This should continue inside a loop.
What I have now is this :
for i = 6000 : 3000 : length(YourVector)
if abs(YourVector(i) - YourVector(i-3000)) > 10
output = [output, YourVector(i+[0 100 200 300 400 500])];
if abs(YourVector(i+100) - YourVector(i+500)) > 10
output = [output, YourVector(i+[0 600 700 800 900 1000])];
end
else
output = [output, YourVector(i)];
end
I am only able to compare the extreme elements for this, not any of the intermediate elements. Note : This is in continuation to a previous question posted here : https://www.mathworks.com/matlabcentral/answers/374580-increment-loop-index-based-on-a-condition
Any help is appreciated.
Thanks.
0 commentaires
Réponses (1)
Jaynik
le 7 Nov 2024 à 8:00
Hi,
Based on my understanding of the given conditions, I think you are on the right track to generate the output. The logic needs to be extended to handle the intermediate elements. Here is an updated code that should handle the conditions and generate the required output:
output = [];
for i = 6000 : 3000 : length(YourVector)
if abs(YourVector(i) - YourVector(i-3000)) > 10
output = [output, YourVector(i+[0 100 200 300 400 500])];
% Check intermediate elements dynamically
for j = 100:100:500
if abs(YourVector(i+j) - YourVector(i+j-100)) > 10
output = [output, YourVector(i+[600 700 800 900 1000])];
break; % Exit loop if condition is met
end
end
else
output = [output, YourVector(i)];
end
end
The nested loop checks each intermediate element (100th, 200th, 300th, 400th, 500th) dynamically. If any of these elements differ from their preceding element by more than 10, it adds the next 5 samples (600th, 700th, 800th, 900th, 1000th) to the output array and breaks out of the loop.
Hope this helps!
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!