What is the difference between (0:2):4 and 0:(2:4)?

Viewed 45

What is the difference between (0:2):4 and 0:(2:4)? Both neglects the 2nd part of the bracket thus printing values similar to writing (0:4) and (0:2) respectively.

I could generalize from this that the bracket's first element is only working in this vector. But I would like to know the actual reason why is it happening.

1 Answers

the colon operator has lower priority than (), so, matlab first evaluates the vector inside the parenthesis, then, if one of the operands is a vector, colon only takes the first value. here are the evaluation steps:

(0:2):4 -> (0:2)=[0 1 2] -> 0:4 -> [0,1,2,3,4]
0:(2:4) -> (2:4)=[2 3 4] -> 0:2 -> [0,1,2]
Related