
Re: Making a square matrix from two vector
Posted:
Feb 12, 2013 11:50 AM


"james bejon" <jamesbejon@yahoo.co.uk> wrote in message news:kfdpbk$kdo$1@newscl01ah.mathworks.com... > BrunoI 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)
This is intentional behavior.
http://www.mathworks.com/help/matlab/matlab_prog/operators.html#f038155
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.
http://www.mathworks.com/help/matlab/ref/colon.html
"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.
 Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com

