how to divide the range of given integer number

1 次查看(过去 30 天)
Hi guys,
Might the deadline of my problem isn't understandable, so here Im explaining my problem.
Im trying to do a range of given integer number between negative and positive (including zero and assume zero is positive ..this just assumption in my case) equally -meaning by equally that the negative number and positive numbers are equally in the two sides, this means :
GivenNumber is an integer parameter (assume it's bigger than 0 )
lets assume I have number 20, GivenNumber=20 so the output is the range : [-10:9] , implicitly the number of numbers/samples in the range [-10:9] are 20 which it's the Givennumber - you can easily see the negative numbers are equally to the positive numbers (including zero as my assumption as positive).
lets assume I have number 10, GivenNumber=10 so output is the range : [-5:4] , implicitly the number of numbers/samples in the range [-5:4] are 10 which it's the Givennumber - you can easily see the negative numbers are equally to the positive numbers (including zero as my assumption as positive).
lets assume I have number 30, GivenNumber=30 so output is the range : [-15:14] , implicitly the number of numbers/samples in the range [-15:14] are 30 which it's the Givennumber - you can easily see the negative numbers are equally to the positive numbers (including zero as my assumption as positive).
So how can I do a function in matlab to get as parameter : GivenNumber , the output is the range as explained above?
Appreciated !

回答(1 个)

Walter Roberson
Walter Roberson 2020-12-21
for GivenNumber = [20 10 30 3 25]
Walters_Half = GivenNumber/2;
ceil(-Walters_Half) : ceil(Walters_Half)-1
end
ans = 1×20
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
ans = 1×10
-5 -4 -3 -2 -1 0 1 2 3 4
ans = 1×30
-15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
ans = 1×3
-1 0 1
ans = 1×25
-12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12
  2 个评论
Image Analyst
Image Analyst 2020-12-21
Jimmy, you can thank answerers by clicking the "Accept this answer" link to give them reputation points. Thanks in advance.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Types 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by