Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
Drexel University or The Math Forum.
|
|
Remus
Posts:
4
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 = A-diag(B)*ones(size(A));
In essence is the same thing.
|
|
|
|