Re: Matrix filling methods? [message #2689 is a reply to message #2688] |
Fri, 19 August 1994 03:33  |
sjt
Messages: 72 Registered: November 1993
|
Member |
|
|
Andy Nicholas (nicholas@dsuap1) wrote:
:
: I'm trying to speed up some code and hence I am trying
: to get rid of some FOR loops. I need to fill a 2-d square
: matrix. The values above the diagonal are given by one
: formula while those values below the diagonal are given
: by another. this is how i currently fill the matrix:
: for i=0,n-1 do begin
: Matrix(i:*,i-1) = x(i-1)/x(k:*)
: Matrix(i-1,i:*) = y(k:*)/y(k-1)
: endfor
: diag=findgen(n)
: Matrix(diag,diag)=z(diag)
: Does anyone know of a way to speed this up? Maybe a where to find the
: matrix elements above the diagonal and one for below?
: Any help is greatly appreciated,
: Thanks,
: Andy
: nicholas.uap.nrl.navy.mil
:
This should be no problem provied you have enough memory for several
arrays of size n. Here is the way I'd do it:
l = lindgen(n,n)
lc = l mod n
lr = l / n
upper = lc gt lr
lower = lc lt lr
Matrix = fltarr(n,n)
matrix(upper) = ...
matrix(lower) = ...
If you need to include the diagonal then just replace gt or lt with ge or le
--
James Tappin, School of Physics & Space Research
University of Birmingham
sjt@xun8.sr.bham.ac.uk
"If all else fails--read the instructions!"
O__
-- \/`
|
|
|