Remus
Posts:
5
Registered:
7/31/11


Re: subtract vector from matrix rows without for loop
Posted:
Dec 6, 2012 11:49 PM


"Matt J" wrote in message <hesupb$i07$1@fred.mathworks.com>... > "Patrick Diviacco" <patrick.diviacco@gmail.com> wrote in message <hesm3s$3sn$1@fred.mathworks.com>... > > hi, > > > > can I subtract the same vector to each row of a matrix without using a for loop ? > > thanks > > For loops may be optimal (looping along the smallest dimension of the matrix), when the smallest dimension is small enough.
I like Matt's answer... here is another way of doing it, slightly more efficient.
A = magic(3); B = [ 1 2 3]; C = Adiag(B)*ones(size(A));
In essence is the same thing.

