"james bejon" <email@example.com> wrote in message news:firstname.lastname@example.org... > Bruno--I really like this bsxops stuff, and have started using it in some > of my projects at work. In the process, I've realised something which (at > least in my view) is rather odd (though is nothing to do with your > functions), namely the result of: > > (1:10).' + 1:10 > > % the right way: (1:10).' + (1:10)
Parentheses are at level 1 in the precedence order. Transpose is at level 2. Addition is at level 5. The colon operator is at level 6. Therefore this:
(1:10).' + 1:10
computes (1:10) first (level 1 and level 6 inside the parentheses), then transposes the result (level 2), then adds 1 to that result (level 5), then uses that vector as the first input to the colon operator (level 6.) That just takes the first element as documented on the reference page for COLON.
"If you specify nonscalar arrays, MATLAB interprets j:i:k as j(1):i(1):k(1)."
This also explains why your "the right way" resolves the problem; both sets of parentheses at level 1 are processed before the addition at level 5.
This is also why -8^(1/3) gives -2 instead of (1+sqrt(3)*1i) as people expect and sometimes post about here; exponentiation [8^(1/3)] is at level 2 while unary minus [- (8^(1/3))] is at level 3. (-8)^(1/3) gives the complex answer.