Path: news.mathworks.com!not-for-mail
From: "Remus " <remusac@yahoo.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: subtract vector from matrix rows without for loop
Date: Fri, 7 Dec 2012 04:49:15 +0000 (UTC)
Organization: Wright State University
Lines: 16
Message-ID: <k9rsgb$rtv$1@newscl01ah.mathworks.com>
References: <hesm3s$3sn$1@fred.mathworks.com> <hesupb$i07$1@fred.mathworks.com>
Reply-To: "Remus " <remusac@yahoo.com>
NNTP-Posting-Host: www-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1354855755 28607 172.30.248.48 (7 Dec 2012 04:49:15 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 7 Dec 2012 04:49:15 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2956144
Xref: news.mathworks.com comp.soft-sys.matlab:784360

"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.