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.
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.
···
On Sat, Jul 12, 2003 at 01:35:39AM +0900, Qubert wrote:
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
b would be right rotated by 1, because your first index -1 into a is the
last element of a.
b # => [5, 0, 1, 2, 3, 4]
If you just want to skip the first element of a in b that would be a
pretty short solution:
b = a[1, a.size]
···
On 2003-07-12 01:35:39 +0900, Qubert wrote:
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.
–
To teach how to live with uncertainty, yet without being paralyzed by
hesitation, is perhaps the chief thing that philosophy can do.
– Bertand Russell
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.
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.
–
To teach how to live with uncertainty, yet without being paralyzed by
hesitation, is perhaps the chief thing that philosophy can do.
– Bertand Russell
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! :-/
···
On 2003-07-12 04:16:41 +0900, Gennady wrote:
Or just
b = a[1…-1]
–
For every complex problem, there is a solution that is simple, neat, and
wrong.
– H. L. Mencken
And you sacrificed this nice Ruby feature for … what do you call it? Perl?
···
----- Original Message -----
From: “Florian Frank” flori@nixe.ping.de
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, July 11, 2003 12:50 PM
Subject: Re: cross-corr time/array lag problem
On 2003-07-12 04:16:41 +0900, Gennady wrote:
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! :-/
–
For every complex problem, there is a solution that is simple, neat, and
wrong.
– H. L. Mencken
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
···
–
N’attribuez jamais à la malice ce que l’incompétence explique !
– Napoléon