[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Simple array question
From: |
Paul Kienzle |
Subject: |
Re: Simple array question |
Date: |
Mon, 26 May 2003 01:12:48 -0400 |
User-agent: |
Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.3) Gecko/20030312 |
Terry Letsche wrote:
Hello. I'd like to iterate with a for loop to fill an array with values,
but I'm having troubles on a couple fronts.
1) I take it it's not possible to have an array subscribt of 0 with the
2.1.42 release (running under windows)??
Correct. And yes, this will make it painful to port code from
a zero-origin language.
2) Here's something like I'd like to do (generating matrices to do signal
processing with wavelets):
The code I'm trying to convert is Matlab code,
No it isn't.
so some thing like using
n.0 as a variable name don't seem to be allowed, so I thought the next
most logical thing would be to put it those values in an array.
Whatever the language is, it seems to use . to introduce array
subscripts, so yes you should be using arrays.
k=5;
n(0) = 2^k;
for t = 1:k
n(t) = n(0) / 2^t; %% I get an error here about scalars??
endfor;
You need to use ./ rather than / for element-wise division.
Also, you should be using vector operations rather than
loops. The above is equivalent to:
n = 2 .^ [ k : -1 : 0 ];
(up to a change in indexing).
Here's the original code. I think I'm going to have another problem with
the call to the matrix function?
Yes. The matrix function below takes two dimensions
and an anonymous function on the index. You will have
to construct it directly yourself in Octave.
To get good performance, you will have to figure out
how to vectorize the construction. It looks like the
identity matrix with +/- 1/2 on some off-diagonals,
so something like:
Mt = eye(n(1)); # default to identity matrix
do_fortran_indexing=1; # allow index arithmetic
i = [1:n(t+1)/2+1];
j = 2*i-1;
Mt( (j-1)*n(1) + i ) = 1/2; # Mt(i,j) = 1/2
...
I don't know if matix(m,n,proc...) creates an m x n
matrix with dimensions 0...m-1, 0...n-1 or an
m+1 x n+1 matrix with dimensions 0...m, 1...n.
Be careful to use (1/2)*(-1)^j since j is 1-origin
instead of 0-origin.
Note that you will not be able to create a vector of
matrices as defined below. If your version of octave
is new enough (>= 2.1.36?) you will be able to instead
create a "cell-array" of matrices M{1}, M{2}, ..., M{k+1}.
k:=5: n.0:=2^k: for t from 1 to k do n.t:=n.0/2^t: od:
for t from 0 to k-1 do M.t:=matrix(n.0,n.0,proc(i,j) if \
(i<=(n.t/2) and (2*i-1)<=j and j<=(2*i)) then (1/2) elif \
(i>(n.t/2) and i<=n.t and (2*(i-n.t/2)-1)<=j and j<=(2*(i-n.t/2))) \
then (1/2)*(-1)^(j+1) elif \
(i>n.t and i=j) then 1 else 0 fi end: od:
For the following, you will need to use Mp instead of M.
Replace M:=multiply(M.q,M): with Mp = M{q}* Mp;
inverse will work in Octave, but I believe matlab wants inv.
M:=M.0: A:=inverse(M.0):
for q from 1 to k-1 do M:=multiply(M.q,M):
A:=multiply(A,inverse(M.q)): od:
Paul Kienzle
address@hidden
-------------------------------------------------------------
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
-------------------------------------------------------------