Deletion of array value in both positions of separate arrays dependent upon the range of one array

2 vues (au cours des 30 derniers jours)
Im trying to limit an array to within range -3 to 3 but also delete the corresponding value in another array if the value in the first array falls outside range.
A = [1 2 4];
f = [2 1 .1];
gamma = [0 pi/2 pi/4];
time = [0:0.1:10];
for i = 1:3
result = A(1,i)*cos(2*pi*f(1,i)*(time)+gamma(1,i));
if result < 3 & result > -3
plot(time, result); hold on;
elseif result > 3
delete time(1, i)
delete result(1, i)
elseif result < -3
delete time(1, i)
delete result(1, i)
end

Réponses (1)

Jos (10584)
Jos (10584) le 23 Fév 2019
A few remarks:
1. what if result is exactly +3 or -3? (you might want to use <= rather than <)
2. replace the two ELSEIF's by a single ELSE, they have the same effect.
3. remove the values after the loop, by keeping track what to remove. In pseucocode:
RemoveMe = false(1,3)
for ...
if ...
else
RemoveMe(i) = true ;
end
end
time(RemoveMe) = []
4. you could remove the for-loop altogehte, using matlabs vectorisation capabilities
result = A.*cos(2*pi.*f.*time+gamma);
time(abs(result)>3) = []
5. maybe you do not want to remove the values but replace them with NaN, so all lengths etc stay the same
I hope this helps.
~ Jos
  2 commentaires
Daniel James
Daniel James le 23 Fév 2019
Modifié(e) : Daniel James le 23 Fév 2019
Well, im plotting the values of result against time, so if a value falls outside the range i want to remove both the sult and the corresponding time, im not sure how the plotting mechanism would behave if it read NaN.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by