Thread Subject: How to vectorize this?

Subject: How to vectorize this?

From: David Doria

Date: 27 May, 2008 14:31:02

Message: 1 of 6

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

Subject: How to vectorize this?

From: aasim Azooz

Date: 27 May, 2008 15:52:05

Message: 2 of 6

"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
>
> Thanks!
> Dave

if x is an nXm matrix then setting
y=x(1,:)
will give you the first raw of x in y
y=x(2,:)
will give you the second raw
stop going back to old good memories of fortran and basic
Aasim Azooz

Subject: How to vectorize this?

From: David Doria

Date: 27 May, 2008 16:14:02

Message: 3 of 6

yes, i've got that part, but I dont want to get each row at
a time in a loop, I'd like to vectorize the loop

Subject: How to vectorize this?

From: Simon Preston

Date: 27 May, 2008 17:21:01

Message: 4 of 6

"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

Subject: How to vectorize this?

From: David Doria

Date: 27 May, 2008 18:14:02

Message: 5 of 6

haha clearly you wouldn't take the norm of a scalar, that
was an artifact of me simplifying the code before posting
and not deleting things so it still made sense!

ok, now the question is, you used a function like "sum"
which can deal with matrices passed as parameters. If I had
a function that only can operate on once vector at a time
and wanted to make it act like sum does, I would just put a
for loop in it! This has just moved the for loop inside the
function rather than make it behave any differently, right?

Say the function
function MyCross(A, B)
%take cross product of A and B
end

how would I call this in the original loop I posted instead
of the dot product which you replaced by using the sum()
function?

Thanks for the help!

Subject: How to vectorize this?

From: Simon Preston

Date: 27 May, 2008 22:16:01

Message: 6 of 6

"David Doria" <daviddoria@gmail.com> wrote in message
<g1hj1a$e43$1@fred.mathworks.com>...
> haha clearly you wouldn't take the norm of a scalar, that
> was an artifact of me simplifying the code before posting
> and not deleting things so it still made sense!
>
> ok, now the question is, you used a function like "sum"
> which can deal with matrices passed as parameters. If I had
> a function that only can operate on once vector at a time
> and wanted to make it act like sum does, I would just put a
> for loop in it! This has just moved the for loop inside the
> function rather than make it behave any differently, right?
>
> Say the function
> function MyCross(A, B)
> %take cross product of A and B
> end
>
> how would I call this in the original loop I posted instead
> of the dot product which you replaced by using the sum()
> function?

You're right, moving the loop inside the function will do
nothing to make it run faster.

There's no one solution to vectorizing code - only way is to
look and learn how others do it. For your the cross product
example, see how Matlab's vectorized version works:
edit cross

Best wishes, S

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com