MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Jan Simon
on 28 May 2013

Dear Matlab-profis and beginners,

What do you **expect** as result of this:

a = 2:3:4:5:6:7

And this:

b = 2:3:4:5:6:7:8

What reasons do you have for your assumptions?

Letting Matlab find the results is too easy. But even if you do, do you have good arguments for the results? Btw, The results are equal for 6.5, 2009a and 2011b.

*No products are associated with this question.*

Answer by Daniel
on 29 May 2013

Accepted answer

The documentation lists the colon operator as having the 6th highest precedence and says that

*Within each precedence level, operators have equal precedence and are evaluated from left to right*

What is not clear from the precedence documentation is if `a:b:c` has one or two colon operators. The documentation for the colon operator says

*To generate a series that does not use the default of incrementing by 1, specify an additional value with the colon operator (first:step:last).*

which suggests to me that `a:b:c` is a single colon operation (I think this is probably the weakest aspect of the documentation).

The documentation of the `colon` function says

*COLON(J,K) is the same as J:K and COLON(J,D,K) is the same as J:D:K.*

I interpret "the same" to me that the converse is also true and that `J:K` is the same as `colon(J,K)` and `J:D:K` is the same as colon(J,D,K).

Based on this I would assume a left to right substitution of triplets.

a = 2:3:4:5:6:7 = colon(2,3,4):5:6:7 = 2:5:6:7 = colon(2,5,6):7 = 2:7 = colon(2,7) = [2,3,4,5,6,7]

b = 2:3:4:5:6:7:8 = colon(2,3,4):5:6:7:8 = 2:5:6:7:8 = colon(2,5,6):7:8 = 2:7:8 = colon(2,7,8) = 2

For the additional case given in the comment to IA's answer of 2:3:6:7 we need a little more of the documentation

*If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1).*

a = 2:3:6:7 = colon(2,3,6):7 = [2, 5]:7 = colon(2, 7) = [2,3,4,5,6,7]

Jan Simon
on 30 May 2013

Thanks, Daniel, a very nice investigations an explanation.

This is the most confusing, well documented piece of valid Matlab code I've seen yet. I (dis)like the following also, but compared to the stacked colons it is much easier to explain and despite the visual disaster fully intuitive:

.2e2.*2.

Daniel
on 31 May 2013

I am a little surprised that "2." is valid. This leads to even odder constructs like 2.e2 and 2..*2.

Jan Simon
on 1 Jun 2013

Now I've struggled for 2 hours with Matlab's documentation and Google to find a documentation about valid numerical notations. My trials to search ended all at descriptions of s/f/printf() and format(). All I could found was a printed documentation of Sinclair ZX-81 Basic interpreter. This demonstrates a weakness of my search methods.

I'm going to ask the technical support.

Answer by the cyclist
on 29 May 2013

I would expect MATLAB to interpret this left-to-right, grouping in the form

M:I:N

where possible, so

a = 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

b = 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

This is what I get in R2013a.

Jan Simon
on 29 May 2013

Fine.

a = 2:3:6:7 = (2:3:6):7 = [2, 5]:7 = [2, 3, 4, 5, 6, 7]

Then in [a,b]:c the value of b is ignored. This is similar to the frequently appearing mistake: `for k = 1:size(b)`.

I had expected the colon operator with two inputs `M:N` dominates.

the cyclist
on 29 May 2013

As mentioned by Daniel in his answer, your comment that b is ignored seems to be mentioned in the documentation:

*If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1).*

Jan Simon
on 29 May 2013

Thank you, Cyclist. I found an equivalent piece of code in the question of a beginner, who assumed `2:3:4:5:6:7` to be the correct method to create `2:7`. It seemed to be a prove for the correctness, that the expected result is produced. But the different result of `2:3:4:5:6:7:8` looked *strange* for the user, and for me even both have the same level of strangeness.

## 0 Comments