Problem removing negative numbers from my vector using for loop.
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
John Lutz
el 27 de Sept. de 2017
Comentada: OCDER
el 27 de Sept. de 2017
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/167603/image.png)
Not sure what to exactly do from here, as nothing has helped online. Not very good with for loops. Whenever doing this code (as shown) it deletes whatever amount of negatives I have from the start, rather than deleting the actual negative numbers.
Please keep and mind I must use a for loop and input display when doing this. Thank you.
2 comentarios
OCDER
el 27 de Sept. de 2017
Next time, post the code instead of a screen shot - it makes it easy for us to solve your issue via copy and paste. Use the {} code button to make it neat like this:
function y = displayVector
%code here
Respuesta aceptada
OCDER
el 27 de Sept. de 2017
Editada: OCDER
el 27 de Sept. de 2017
Since you need a loop, consider creating a logical index matrix and then changing this where the negative numbers are. After the loop, delete the negative number from y.
function y = displayVector
y = input('Display vector: ');
DelIdx = zeros(1, numel(y), 'logical');
for k = 1:numel(y)
if y(k) < 0
DelIdx(k) = 1;
end
end
y(DelIdx) = [];
2 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!