MATLAB: 1:2:10100:-25:0 gives 1, how

coloninterpretationvector

Why 1 is returned while sometimes "Empty matrix" is returned?
The highlighted text in the image.
Screenshot (303).png

Best Answer

1:2:10100:-25:0 is correct Matlab syntax, but might be interpreted unexpectedly. It is evaluated from left to right, as usual in Matlab:
x = 1:2:10100:-25:0
= (1:2:10100):-25:0
The documentation of colon says: If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1). Then the above is interpreted as:
= 1:-25:0
= 1
If more elements are appended:
x = 2:3:4:5:6:7
= (2:3:4):5:6:7
= (2):5:6:7
= (2:5:6):7
= 2:7
= [2,3,4,5,6,7]
and
x = 2:3:4:5:6:7:8
= (2:3:4):5:6:7:8
= (2):5:6:7:8
= (2:5:6):7:8
= 2:7:8
= 2