Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to vectorize this?
Date: Tue, 27 May 2008 17:21:01 +0000 (UTC)
Organization: University of Nottingham
Lines: 34
Message-ID: <g1hftt$s4m$1@fred.mathworks.com>
References: <g1h5v5$o85$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1211908861 28822 172.30.248.37 (27 May 2008 17:21:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 27 May 2008 17:21:01 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1072079
Xref: news.mathworks.com comp.soft-sys.matlab:470661



"David Doria" <daviddoria@gmail.com> wrote in message
<g1h5v5$o85$1@fred.mathworks.com>...
> 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

Along these lines:
b = Points - repmat(location, [size(Points,1) 1]);
L(:,1) = sum(repmat(forward, [size(Points,1) 1])).*b,2);
L(:,2) = Points(counter,2)

(Not sure why you find the norm of a the scalar 'projected')

Best wishes, S