Im trying to find out how to take out a number that's above 10 in a vector of v=[-5, 2, -4, 1; 0, -9, -9, 20; 50, 89, 99, 100] using for loop and using user defined functions

1 comentario

KALYAN ACHARJYA
KALYAN ACHARJYA el 16 de Nov. de 2018
Editada: KALYAN ACHARJYA el 16 de Nov. de 2018
No need of for loop? Is it must to used?

Iniciar sesión para comentar.

 Respuesta aceptada

madhan ravi
madhan ravi el 16 de Nov. de 2018
Editada: madhan ravi el 16 de Nov. de 2018

1 voto

without loop (efficient)
v=[-5, 2, -4, 1; 0, -9, -9, 20; 50, 89, 99, 100]
result = numbergreaterthan10(v) %function calling
function result = numbergreaterthan10(x) %function definition
result= x(x>10);
end
with loop (not efficient)
v=[-5, 2, -4, 1; 0, -9, -9, 20; 50, 89, 99, 100]
result = numbergreaterthan10(v) %function calling
function result = numbergreaterthan10(x) %function definition
for i = 1:numel(x)
if x(i)>10
result(i)=x(i);
else
continue
end
end
result=nonzeros(result);
end

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 16 de Nov. de 2018

Editada:

el 16 de Nov. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by