[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Request help speeding up script
From: |
Muthiah Annamalai |
Subject: |
Re: Request help speeding up script |
Date: |
Mon, 25 Jun 2007 21:24:50 -0500 |
On 6/25/07, Rob Teasdale <address@hidden> wrote:
Hi all,
I am still learning Octave, and I am still discovering new ways of optimising
code. I have managed to vectorise a lot of my 'for' loops, and I have managed
to speed my small script up dramatically, although I am having trouble with
this last code snippet below. What I am trying to achieve is to calculate the
area of a quadrilateral by the vector cross product of its diagonals. X, Y and
Z contain the points that describe my mesh. I am sure that there is a way to
vectorise at least part of this, and I would really appreciate any suggestions,
[R C] = size(X); % all matrices the same size
row = 0;
for i = 1:(R-1)
for j = 1:(C-1)
P4 = [X(i,j) Y(i,j) Z(i,j)];
P3 = [X(i,j+1) Y(i,j+1) Z(i,j+1)];
P2 = [X(i+1,j+1) Y(i+1,j+1) Z(i+1,j+1)];
P1 = [X(i+1,j) Y(i+1,j) Z(i+1,j)];
p = P2-P4;
q = P3-P1;
row++
r(row,:) = 0.5 * (cross(p,q)); % calculate the area
endfor
endfor
Thanks in advance
Rob
I think the inner loop will be vectorized as,
P4 = [X(i,1:C-1)' Y(i,1:C-1)' Z(i,1:C-1)'];
P3 = [X(i,2:C)' Y(i,2:C)' Z(i,2:C)'];
P2 = [X(i+1,2:C)' Y(i+1,2:C)' Z(i+1,2:C)'];
P1 = [X(i+1,1:C-1)' Y(i+1,1:C-1)' Z(i+1,1:C-1)'];
p = P2-P4;
q = P3-P1;
r =[r; 0.5*cross(p,q) ]; % calculate the area
-Muthu