help-octave
[Top][All Lists]

## Vectorizing loops

 From: andrewcd Subject: Vectorizing loops Date: Fri, 4 Nov 2011 23:57:07 -0700 (PDT)

```Hi All,

I'm relatively new to working with large datasets, and I need to break the
habit of doing everything in loops.  I'd appreciate if you all could help me
with one example to get me started.

I have a 260x260x8 matrix.  The top row is x coordinates, the first column
is y coordinates, the depth dimension is time.  I want to get the matrix
into the following format so that I can do panel data regressions:

x1 y1 t1 val
x1 y1 t2 val
x1 y2 t1 val
x1 y2 t2 val
x2 y1 t1 val
x2 y1 t2 val
etc

My code (which works) with all of the loops is:
G=[]
for j = 1:columns(N)
disp(j)
disp ('of 259')
for i = 1:rows(N)
for k = 1:size(N,3)
G(size(G,1)+1,4) = N(i,j,k);
G(size(G,1),1) = Nxy(1,j,k);
G(size(G,1),2) = Nxy(i,1,k);
G(size(G,1),3) = k;
end end end

(N is the matrix without the coordinates on the sides)

There must be a better way.  Hints on how to do so would be much appreciated
-- I'll eventually need to do this operation many, many times.  Thanks in

Cheers,
Andrew

--
View this message in context:
http://octave.1599824.n4.nabble.com/Vectorizing-loops-tp3992596p3992596.html
Sent from the Octave - General mailing list archive at Nabble.com.

```