I came across some MATLAB syntax with a colon that I don’t fully understand.
First Question:
The expression: 0:pi/4:pi
results in the answer: 0 0.7854 1.5708 2.3562 3.1416
Why is this the case? I thought that colon operator is used as a quick way to refer to indices so that we don’t have to write out the full list. (e.g. 1:3 -> 1 2 3)
Second Question:
Similar to above, say if I have a matrix X = [1 2 3 4 5 6 7 8 9]. How can I interpret the expression X(:,1:3)? Specifically, what does the colon operator without the left and right numbers mean?
Actually
a:bgenerates a vector. You could use it as index only because the(...)accepts a list also, e.g.Now, the
a:b:csyntax is equivalent to[a, a+b, a+2*b, ...]untilc, e.g.which explains what you get in
0:pi/4:pi.A lone
:selects the whole axes (row/column), e.g.See the official MATLAB doc on colon (:) for detail.