필터 지우기
필터 지우기

Writing a simple funtion

조회 수: 6 (최근 30일)
Kristopher
Kristopher 2014년 6월 20일
편집: Roger Stafford 2014년 6월 20일
I need to write a function in which you enter 2 inputs, and the output is the sum of the integers between those numbers. for example, if x=(1,4) y should equal (1+2+3+4) (y=10) It should give the same output when x=(4,1). This should be able to be done using a forloop, but im not sure how to do it.

답변 (1개)

Roger Stafford
Roger Stafford 2014년 6월 20일
편집: Roger Stafford 2014년 6월 20일
The 'ceil' and 'floor' are used here in case the inputs are not integers.
function s = sumintegers(a,b)
if a <= b
s = sum(ceil(a):floor(b));
else
s = sum(ceil(b):floor(a));
end
return

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by