How do i step from a bigger number to a smaller number ?

I want to create an array with two numbers like this;
Xprime = x1 : 0.01 : x2
but when i make x1 > x2 i get an empty array.
Is there a solution to this ?

 채택된 답변

Image Analyst
Image Analyst 2021년 6월 30일
In general, if you don't know what x1 and x2 are, and which one will be bigger, you can do
x1 = 3
x2 = 2
inc = 0.01;
xPrime = x1 : sign(x2-x1) * inc : x2
If you know how many steps you want between the two, you can use the linspace() function instead:
numSteps = 10;
xPrime = linspace(x1, x2, numSteps)

추가 답변 (2개)

Akshit Bagde
Akshit Bagde 2021년 6월 30일
If you want to create a decrement array, you should use
Xprime = x1:-0.01:x2;
KSSV
KSSV 2021년 6월 30일
x = 2:-0.01:1
x = 1×101
2.0000 1.9900 1.9800 1.9700 1.9600 1.9500 1.9400 1.9300 1.9200 1.9100 1.9000 1.8900 1.8800 1.8700 1.8600 1.8500 1.8400 1.8300 1.8200 1.8100 1.8000 1.7900 1.7800 1.7700 1.7600 1.7500 1.7400 1.7300 1.7200 1.7100

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2019b

질문:

2021년 6월 30일

댓글:

2021년 7월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by