How to operate on 2 arrays simultaneously? - RUBY

This is a discussion on How to operate on 2 arrays simultaneously? - RUBY ; There has got to be a more elegant solution than this. Suppose I have 2 identical length arrays that I want to add together to produce a third array. The best solution I've come up with is: c = (0...a.size).map ...

1. How to operate on 2 arrays simultaneously?

There has got to be a more elegant solution than this. Suppose I have
2 identical length arrays that I want to add together to produce a
third array. The best solution I've come up with is:

c = (0...a.size).map {|i| a[i] + b[i]}

Any suggestions?

--wpd

2. Re: How to operate on 2 arrays simultaneously?

On Sep 10, 2008, at 10:42 AM, Patrick Doyle wrote:

> There has got to be a more elegant solution than this. Suppose I have
> 2 identical length arrays that I want to add together to produce a
> third array. The best solution I've come up with is:
>
> c = (0...a.size).map {|i| a[i] + b[i]}
>
> Any suggestions?

a.zip(b).map { |l, r| l + r }

Hope that helps.

James Edward Gray II

3. Re: How to operate on 2 arrays simultaneously?

On Sep 10, 2008, at 9:42 AM, Patrick Doyle wrote:

> There has got to be a more elegant solution than this. Suppose I have
> 2 identical length arrays that I want to add together to produce a
> third array. The best solution I've come up with is:
>
> c = (0...a.size).map {|i| a[i] + b[i]}
>
> Any suggestions?

cfp:~ > cat a.rb
a = 0,1,2
b = 3,4,5

c = a.zip(b).map{|pair| pair.first + pair.last}
p c

c = Array.new(a.size){|i| a[i] + b[i]}
p c

cfp:~ > ruby a.rb
[3, 5, 7]
[3, 5, 7]

the second is far more efficient for large arrays - in terms of memory.

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama

4. Re: How to operate on 2 arrays simultaneously?

Take a look at the zip method.

Jamey

On Wed, Sep 10, 2008 at 11:59 AM, DanDiebolt.exe <dandiebolt@yahoo.com> wrote:
>>There has got to be a more elegant solution than this.

>
> I use .each_with_index to walk parallel arrays:
>
> a=[1,2,3]
> b=%w(x y z)
> a.each_with_index do |item,index|
> puts "item=#{item} index=#{index} a[#{index}]=#{a[index]} b[#{index}]=#{b[index]}"
> end
>
>
>

5. Re: How to operate on 2 arrays simultaneously?

DanDiebolt.exe <dandiebolt@yahoo.com> wrote:

> >There has got to be a more elegant solution than this.

>
> I use .each_with_index to walk parallel arrays:

So did I until the other answers in this thread taught me about zip!
m.

--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leop...stomizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com

6. Re: How to operate on 2 arrays simultaneously?

On Sep 10, 2008, at 10:28 AM, matt neuburg wrote:

> So did I until the other answers in this thread taught me about
> zip!

be careful with zip

a = big
b = big

huge_new_array = a.zip(b)

another_huge_new_array = huge_new_array.each{|a,b| a + b}

using something like each_with_index constructs no new array

a @ http://codeforpeople.com/
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama

7. Re: How to operate on 2 arrays simultaneously?

> a = 0,1,2
> b = 3,4,5
>
> c = a.zip(b).map{|pair| pair.first + pair.last}

Arrghh!
I looked at zip and, for some reason completely missed the fact that
it constructs an array of arrays. For some reason, I decided that it
simply interweaved the two arrays. That's what I was looking for.

> c = Array.new(a.size){|i| a[i] + b[i]}
> the second is far more efficient for large arrays - in terms of memory.

ok, I can see that.

Thanks for the tips.

--wpd

8. Re: How to operate on 2 arrays simultaneously?

On 10.09.2008 17:55, ara.t.howard wrote:

> c = a.zip(b).map{|pair| pair.first + pair.last}

> c = Array.new(a.size){|i| a[i] + b[i]}

> the second is far more efficient for large arrays - in terms of memory.

Well, if you want to avoid the temp Array you can do

irb(main):001:0> a = 0,1,2
=> [0, 1, 2]
irb(main):002:0> b = 3,4,5
=> [3, 4, 5]
irb(main):003:0> a.to_enum(:zip, b).map {|x,y| x + y}
=> [3, 5, 7]

Cheers

robert

9. Re: How to operate on 2 arrays simultaneously?

On Wed, Sep 10, 2008 at 9:35 AM, ara.t.howard <ara.t.howard@gmail.com> wrote:
>
> be careful with zip
>
> a = big
> b = big
>
> huge_new_array = a.zip(b)
>
> another_huge_new_array = huge_new_array.each{|a,b| a + b}

zip takes a block, even in 1.8, though sadly it yields without
accumulating so you have to do it yourself. but this works:

c = []; a.zip(b) {|i, j| c << f(i,j)}

martin

10. Re: How to operate on 2 arrays simultaneously?

ara.t.howard <ara.t.howard@gmail.com> wrote:

> be careful with zip
>
> a = big
> b = big
>
> huge_new_array = a.zip(b)

Everything in Ruby is a pointer. If a[0] is a pointer to a "big long
string", then a.zip(b)[0][0] is a pointer to the very same "big long
string" - not a copy of the string. So creating the "huge_new_array"
just creates some new pointers, right? And pointers are very small. So
for huge_new_array to be a problem, the existence of a and b would have
to have been problematic to start with - meaning that they would have to
have huge length. The amount of *data* in the story (stuff like "big
long string") is not increased.

m.
--
matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/
Leopard - http://www.takecontrolbooks.com/leop...stomizing.html
AppleScript - http://www.amazon.com/gp/product/0596102119
Read TidBITS! It's free and smart. http://www.tidbits.com