Deletion of array value in both positions of separate arrays dependent upon the range of one array
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Jos (10584)
2019-2-23
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 个评论
另请参阅
类别
在 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!