Deletion of array value in both positions of separate arrays dependent upon the range of one array
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
Jos (10584)
el 23 de Feb. de 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 comentarios
Jos (10584)
el 25 de Feb. de 2019
Just try it:
x = 1:10
y = x.^2
y(7) = NaN
plot(x,y,'bo-')
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!