user defined functions help
조회 수: 3 (최근 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
KALYAN ACHARJYA
2018년 11월 16일
편집: KALYAN ACHARJYA
2018년 11월 16일
No need of for loop? Is it must to used?
채택된 답변
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!