Path: news.mathworks.com!not-for-mail
From: "David Doria" <daviddoria@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: How to vectorize this?
Date: Tue, 27 May 2008 14:31:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 23
Message-ID: <g1h5v5$o85$1@fred.mathworks.com>
Reply-To: "David Doria" <daviddoria@gmail.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1211898662 24837 172.30.248.35 (27 May 2008 14:31:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 27 May 2008 14:31:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1105197
Xref: news.mathworks.com comp.soft-sys.matlab:470626



Frequently I would like to operate on a matrix one row at a
time. I always give up and resort to a for loop in a case
like this.  Can someone explain how you would vectorize this
loop?

forward = [1 2 3];
location = [4 5 6];

%Points is a matrix like this:
%x1 y1 z1
%x2 y2 z2
%x3 y3 z3
%etc

for counter = 1:size(Points,1)
  b = Points(counter, :) - location;
  projected = dot(forward,b);
  L(counter,1) = norm(projected);
  L(counter,2) = Points(counter,2);
end

Thanks!
Dave