I'd like to see the various solutions for coming up with code to
transpose a matrix. ie. make
a = [[1,2],[1,3],[2,4]]
look like
aT = [[1,1,2],[2,3,4]]
I haven't had time to do a solution myself, but I think it would be
interesting to see how some of you guys would tackle it while I
derive something.
For those of us who didn’t know Matrix was built in, my first crack at it
was…
irb(main):011:0> a = [[1,2],[3,4],[5,6]]
[[1, 2], [3, 4], [5, 6]]
irb(main):012:0> c = []; a[0].size.times{c.push([])}; a.each{|b| bi = 0;
b.each
{|b1| c[bi].push(b1); bi += 1}}; c
[[1, 3, 5], [2, 4, 6]]
···
-----Original Message-----
From: Daniel Bretoi [mailto:lists@debonair.net]
Sent: Wednesday, July 31, 2002 11:24 AM
To: ruby-talk ML
Subject: matrix challange
Hi all,
I'd like to see the various solutions for coming up with code to
transpose a matrix. ie. make
a = [[1,2],[1,3],[2,4]]
look like
aT = [[1,1,2],[2,3,4]]
I haven't had time to do a solution myself, but I think it would be
interesting to see how some of you guys would tackle it while I
derive something.
-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==---------- http://www.newsfeed.com The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----
Sorry all, I actually checked for a matrix class, but I must have missed
it (. Anyway, what I would be interested in is how you would approach the
problem WITHOUT using the matrix class.
db
···
On Thu, Aug 01, 2002 at 01:39:16AM +0900, Ville Mattila wrote:
I'd like to see the various solutions for coming up with code to
transpose a matrix. ie. make
a = [[1,2],[1,3],[2,4]]
look like
aT = [[1,1,2],[2,3,4]]
I haven't had time to do a solution myself, but I think it would be
interesting to see how some of you guys would tackle it while I
derive something.
funny you should ask. i just had to do that myself.
aT = []
a.length.times do |y|
a[y].length.times do |x|
aT[x] = [] unless aT[x]
aT[x][y] = a[y][x]
end
end
the only thing somewhat annoying about this is having to specify aT[x] =
[]. wish ruby could just pick up on that when i try to use it as such.
There’s nothing for Ruby to pick up on. If Ruby’s first exposure to
a variable is:
var[10] = “hi”
all it knows is that var responds to a method called #[]= (where
var[10]=“hi” is short for var.[]=(10,“hi”)). There’s absolutely no
indication that var is supposed to be an Array (or a Hash, or a
String, or a NewThingy).
However… there’s a more concise way to do the “initialize if
necessary” thing:
Are there any other sources for knowing builtin classes/modules?
still new,
db
···
On Thu, Aug 01, 2002 at 01:47:55AM +0900, Daniel Bretoi wrote:
Sorry all, I actually checked for a matrix class, but I must have missed
it (. Anyway, what I would be interested in is how you would approach the
problem WITHOUT using the matrix class.
db
On Thu, Aug 01, 2002 at 01:39:16AM +0900, Ville Mattila wrote:
I'd like to see the various solutions for coming up with code to
transpose a matrix. ie. make
a = [[1,2],[1,3],[2,4]]
look like
aT = [[1,1,2],[2,3,4]]
I haven't had time to do a solution myself, but I think it would be
interesting to see how some of you guys would tackle it while I
derive something.
I guess using one of my own libraries doesn’t qualify either, but
here it goes:
require ‘enumerables’
a = [[1,2],[1,3],[2,4]]
aT = Enumerables.new( *a ).collect { | x | x } )
p aT # => [[1, 1, 2], [2, 3, 4]]
The Enumerables class takes a list of enumerables and traverses
them in parallel. In this case, each row in the original matrix is one
of those enumerables.
You can find a similar class called SyncEnumerator in the ruby cvs
at
Sorry all, I actually checked for a matrix class, but I must have
missed it (. Anyway, what I would be interested in is how you would
approach the problem WITHOUT using the matrix class.
Sorry all, I actually checked for a matrix class, but I must have
missed it (. Anyway, what I would be interested in is how you would
approach the problem WITHOUT using the matrix class.
I guess using one of my own libraries doesn’t qualify either, but
here it goes:
Ditto, but anyway, using Enumerable Tools on RAA:
require ‘enum/op’
a = [[1,2],[1,3],[2,4]]
aT = EnumerableOperator::diagonal(*[[1,2],[1,3],[2,4]]).to_a