cross-corr time/array lag problem

Q

Qubert

I am starting to find the "Way" now, but I have a problem.
I am running cross correlations using Ruby, so first I set up the time
series data to be in columns with rows being the same time.
Now I am trying to lag one column to the other by:
a = [0, 1, 2, 3, 4, 5]
b = a.each_index do |j|
a[j-1]
end

But afterward a = b with no lag.
a print a[j]," ",a[j-1] shows this works
but b is being stored as the original "a" not a modified "a" with an
hour lag.

Why is b not being set with a lag? I can not .collect because I need
to manupilate the index not the contents of the array.

Any help would be appreciated.
Thanks,
Qubert
 
B

Brian Candler

I am starting to find the "Way" now, but I have a problem.
I am running cross correlations using Ruby, so first I set up the time
series data to be in columns with rows being the same time.
Now I am trying to lag one column to the other by:
a = [0, 1, 2, 3, 4, 5]
b = a.each_index do |j|
a[j-1]
end

But afterward a = b with no lag.

Yep... all you have done is evaluate a[j-1], and then discard the results.
You would need to use 'collect' to gather the results into a new array (in
which case keep track of the index yourself in a separate variable), or else

b = []
a.each_index do |j|
b << a[j-1] # beware boundary condition :)
end

However there's an easier solution:

b = a.dup
b.shift

shifts b[1] to b[0], b[2] to b[1] etc; or b.unshift(val) sets b[0] to val
and shifts the rest along.

Regards,

Brian.
 
G

Gennady

And you sacrificed this nice Ruby feature for ... what do you call it? Perl?
;-)

----- Original Message -----
From: "Florian Frank" <[email protected]>
To: "ruby-talk ML" <[email protected]>
Sent: Friday, July 11, 2003 12:50 PM
Subject: Re: cross-corr time/array lag problem

Or just

b = a[1..-1]

Yes. But I don't do that any more, because I started to use it in Perl's
array slices where this doesn't work. In Perl just an empty array is
returned. It took me a while before I found that bug! :-/
 
F

Florian Frank

And you sacrificed this nice Ruby feature for ... what do you call it?
Perl? ;-)

I have or better had to: I was bribed. Perl is the past and Ruby is the
future. I really hope so...
 
J

Josef 'Jupp' Schugt

Saluton!

* Gennady; 2003-07-11, 20:45 UTC:
And you sacrificed this nice Ruby feature for ... what do you call
it? Perl?

The full name of that encryption tool is 'Pathologically Eclectic
Rubbish Lister'. It was derived from AWK (short for awkward) and sed
(this refers to the medicin you need if you dare using 'sed' - a
sedativum).

Gis,

Josef 'Jupp' Schugt
 
M

Martin DeMello

Qubert said:
I am starting to find the "Way" now, but I have a problem.
I am running cross correlations using Ruby, so first I set up the time
series data to be in columns with rows being the same time.
Now I am trying to lag one column to the other by:
a = [0, 1, 2, 3, 4, 5]
b = a.each_index do |j|
a[j-1]
end

But afterward a = b with no lag.
a print a[j]," ",a[j-1] shows this works
but b is being stored as the original "a" not a modified "a" with an
hour lag.

Why is b not being set with a lag? I can not .collect because I need
to manupilate the index not the contents of the array.

each_index doesn't have an implicit collect, and its return value is the
array itself (not sure why, but I can't remember needing a return value
anyway). What you need is:

b = []
a.each_index {|j| b[j] = a[j-1]}

or,

b = a.dup.unshift(nil)

or (drumroll)

b = a.map_with_index {|e, i| a[i-1]}

had we a map with index which we don't :)

though beware of wraparound - a[-1] returns the last element.

martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top