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

1 view (last 30 days)
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

Answers (1)

Jos (10584)
Jos (10584) on 23 Feb 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 Comments
Daniel James
Daniel James on 23 Feb 2019
Edited: Daniel James on 23 Feb 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.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!