[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to operate on 2 arrays simultaneously?

Patrick Doyle

9/10/2008 3:43:00 PM

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

6 Answers

James Gray

9/10/2008 3:52:00 PM

0

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

ara.t.howard

9/10/2008 3:55:00 PM

0


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://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Patrick Doyle

9/10/2008 4:43:00 PM

0

> 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

Robert Klemme

9/10/2008 4:59:00 PM

0

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

Peter Bunyan

9/11/2008 7:03:00 PM

0

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?
>
> --wpd

http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/M...
--
Posted via http://www.ruby-....

Armin Armbruster

11/7/2008 8:53:00 PM

0

I just ran into a similar problem, and I thought it fits into this
thread.

The Vector class defines the collect2() and collect2!() methods, which
conceptually I like very much. So I thought I could just use the same
concept and extend Array like this:

class Array
def collect2(v) # :yield: e1, e2
Array.Raise ErrDimensionMismatch if size != v.size
(0 .. size - 1).collect do
|i|
yield self[i], v[i]
end
end

def collect2!(v) # :yield: e1, e2
Array.Raise ErrDimensionMismatch if size != v.size
(0 .. size - 1).collect do
|i|
self[i] = yield self[i], v[i]
end
end
end

After this I can simply do:
a = [1,2,3]
b = [4,5,6]
a.collect2(b){|e1,e2| e1+e2}
=> [5, 7, 9]

Does anybody see anything wrong with this?
If not, it seems such a handy concept that I'm surprised these are not
standard methods of Array. I've had this problem in various forms over
the past and typically reverted to Array#each_index, always thinking
that this is too clumsy for Ruby, but until now I never took the time to
come up with something more elegant (not that I want to take any credit
for this suggestion as I simply copied what's already in Vector).




Todd Benson wrote:
> On Thu, Sep 11, 2008 at 2:02 PM, Peter Bunyan <peter.bunyan@gmail.com>
> wrote:
>>
>> http://www.ruby-doc.org/stdlib/libdoc/matrix/rdoc/classes/M...
>
> Yeah, that's probably what I would do for a simple script. I haven't
> checked memory usage, but it is elegant IMHO. Probably best for very
> large arrays.
>
> a = 1, 2, 3, 4
> b = 4, 3, 2, 1
> (Matrix[a] + Matrix[b]).to_a.flatten
>
> => [5, 5, 5, 5]
>
> Todd

--
Posted via http://www.ruby-....