user defined functions help
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
madhan ravi
2018-11-16
编辑:madhan ravi
2018-11-16
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 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!