# 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.
``````
···

“Then we typed the G, and the system crashed”…

I'd like to see the various solutions for coming up with code to
transpose a matrix. ie. make

pigeon% ruby -rmatrix -e 'a = Matrix[[1,2],[1,3],[2,4]]; p a.transpose'
Matrix[[1, 1, 2], [2, 3, 4]]
pigeon%

Guy Decoux

I would do this:

irb(main):001:0> require 'matrix’
true
irb(main):002:0> a = [[1,2],[1,3],[2,4]]
[[1, 2], [1, 3], [2, 4]]
irb(main):003:0> aT = Matrix[*a].transpose.to_a
[[1, 1, 2], [2, 3, 4]]

Paul

···

On Thu, Aug 01, 2002 at 01:23:41AM +0900, Daniel Bretoi 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]]

Hi –

···

On Thu, 1 Aug 2002, Daniel Bretoi wrote:

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]]

require ‘matrix’

a = Matrix[[1,2],[1,3],[2,4]]
aT = a.t

David

David Alan Black
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Isn’t there a Matrix class in the distro?

–Gavin

···

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.

“Then we typed the G, and the system crashed”…

Daniel Bretoi lists@debonair.net writes:

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.

irb
irb(main):001:0> require 'matrix’
true
irb(main):002:0> foo = Matrix[ [1,2],[1,3],[2,4]]
Matrix[[1, 2], [1, 3], [2, 4]]
irb(main):003:0> foo.transpose
Matrix[[1, 1, 2], [2, 3, 4]]
irb(main):004:0>

``````    That wasn't so hard.
``````
• Ville

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.
``````

“Then we typed the G, and the system crashed”…

``````      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
``````

[]. wish ruby could just pick up on that when i try to use it as such.

~transami

···

On Wed, 2002-07-31 at 10:23, Daniel Bretoi wrote:

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.

“Then we typed the G, and the system crashed”…

~transami

a = [[1,2],[1,3],[2,4]]

look like

aT = [[1,1,2],[2,3,4]]

ruby -rmatrix -e’puts Matrix[[1,2],[1,3],[2,4]].transpose’

-----------== 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:

Daniel Bretoi lists@debonair.net writes:

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.
``````

irb
irb(main):001:0> require 'matrix’
true
irb(main):002:0> foo = Matrix[ [1,2],[1,3],[2,4]]
Matrix[[1, 2], [1, 3], [2, 4]]
irb(main):003:0> foo.transpose
Matrix[[1, 1, 2], [2, 3, 4]]
irb(main):004:0>

``````    That wasn't so hard.
``````
• Ville

“Then we typed the G, and the system crashed”…

Hi –

``````      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
``````

[]. 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:

aT ||= []

which is quite a common Ruby idiom.

David

···

On Thu, 1 Aug 2002, Tom Sawyer wrote:

David Alan Black
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

I’ve been looking at the PP’s Guide and I can’t seem to see it in there.
(http://www.rubycentral.com/book/builtins.html)

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:

Daniel Bretoi lists@debonair.net writes:

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.
``````

irb
irb(main):001:0> require 'matrix’
true
irb(main):002:0> foo = Matrix[ [1,2],[1,3],[2,4]]
Matrix[[1, 2], [1, 3], [2, 4]]
irb(main):003:0> foo.transpose
Matrix[[1, 1, 2], [2, 3, 4]]
irb(main):004:0>

``````    That wasn't so hard.
``````
• Ville

“Then we typed the G, and the system crashed”…

“Then we typed the G, and the system crashed”…

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

http://www.ruby-lang.org/~knu/cgi-
bin/cvsweb.cgi/~checkout~/rough/lib/generator.rb?rev=1.6

Regards,
Pit

···

On 1 Aug 2002, at 1:47, 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.

There are a few. One of my favorites is:

ruby -rrbconfig -e ‘puts Dir.entries(Config::CONFIG[“rubylibdir”])’

though the sources could still use some docs

There are also some good books:

http://www.rubygarden.org/ruby?RubyBookList

Paul

···

On Thu, Aug 01, 2002 at 02:02:35AM +0900, Daniel Bretoi wrote:

I’ve been looking at the PP’s Guide and I can’t seem to see it in there.
(http://www.rubycentral.com/book/builtins.html)

Are there any other sources for knowing builtin classes/modules?

still new,

Pit Capitain wrote:

···

On 1 Aug 2002, at 1:47, 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.

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

p aT