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

7 views (last 30 days)
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 ?

Accepted Answer

Image Analyst
Image Analyst on 30 Jun 2021
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)

More Answers (2)

Akshit Bagde
Akshit Bagde on 30 Jun 2021
If you want to create a decrement array, you should use
Xprime = x1:-0.01:x2;

KSSV
KSSV on 30 Jun 2021
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

Community Treasure Hunt

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

Start Hunting!

Translated by