help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Matrix construction without loops


From: Mike Miller
Subject: Re: Matrix construction without loops
Date: Tue, 18 Feb 2003 00:45:54 -0600 (CST)

On Tue, 18 Feb 2003 address@hidden wrote:

> I am trying to construct a matrix with rows based on a vector without
> using any sort of loops.
>
> given a vector [a,b,c,d], i want to create a matrix:
> [a, b, c, d;
>  b, c, d, a;
>  c, d, a, b;
>  d, a, b, c]
>
> so each the positions of each value in the row cycles one place to the
> left each row.
> For small vectors i can use a loop without any hassles but i am plan to
> be using quite sizable vectors so i want to cut down on run time.
> Any suggestions?


How about this?  Suppose the vector is a row vector of length N.  Here's
one of length 20:

y =

   9   5   3   7   3   6   6   5   6   7   7   3   7   1   0   6  10   5   0   1

Then...

N=length(y);
x=ones(N,1)*[1:N];
reshape(y(rem(x+x'-2,N)+1),N,N);

ans =

   9   5   3   7   3   6   6   5   6   7   7   3   7   1   0   6  10   5   0   1
   5   3   7   3   6   6   5   6   7   7   3   7   1   0   6  10   5   0   1   9
   3   7   3   6   6   5   6   7   7   3   7   1   0   6  10   5   0   1   9   5
   7   3   6   6   5   6   7   7   3   7   1   0   6  10   5   0   1   9   5   3
   3   6   6   5   6   7   7   3   7   1   0   6  10   5   0   1   9   5   3   7
   6   6   5   6   7   7   3   7   1   0   6  10   5   0   1   9   5   3   7   3
   6   5   6   7   7   3   7   1   0   6  10   5   0   1   9   5   3   7   3   6
   5   6   7   7   3   7   1   0   6  10   5   0   1   9   5   3   7   3   6   6
   6   7   7   3   7   1   0   6  10   5   0   1   9   5   3   7   3   6   6   5
   7   7   3   7   1   0   6  10   5   0   1   9   5   3   7   3   6   6   5   6
   7   3   7   1   0   6  10   5   0   1   9   5   3   7   3   6   6   5   6   7
   3   7   1   0   6  10   5   0   1   9   5   3   7   3   6   6   5   6   7   7
   7   1   0   6  10   5   0   1   9   5   3   7   3   6   6   5   6   7   7   3
   1   0   6  10   5   0   1   9   5   3   7   3   6   6   5   6   7   7   3   7
   0   6  10   5   0   1   9   5   3   7   3   6   6   5   6   7   7   3   7   1
   6  10   5   0   1   9   5   3   7   3   6   6   5   6   7   7   3   7   1   0
  10   5   0   1   9   5   3   7   3   6   6   5   6   7   7   3   7   1   0   6
   5   0   1   9   5   3   7   3   6   6   5   6   7   7   3   7   1   0   6  10
   0   1   9   5   3   7   3   6   6   5   6   7   7   3   7   1   0   6  10   5
   1   9   5   3   7   3   6   6   5   6   7   7   3   7   1   0   6  10   5   0


I think it works for any N.  You can make an octave function that takes
the row vector (y) as input at produces the desired matrix as output.

Best,

Mike

-- 
Michael B. Miller, Ph.D.
Assistant Professor
Division of Epidemiology
University of Minnesota
http://taxa.epi.umn.edu/~mbmiller/



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]