# Adding, multiplying array

**URL:** <https://www.ruby-forum.com/t/adding-multiplying-array/169713>\
**Category:** Ruby\
**Created:** [June 7, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713 "2009-06-07T16:17:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [June 7, 2009, 4:17pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713/1 "2009-06-07T16:17:42Z")

</div>

Hi.

I want to multply all elements of two arrays and add that to all  
elements of  
the third array like this:

c+=a\*b

c=[1,1,1]  
a=[2,2,2]  
b=[3,3,3]

c=[7,7,7] # (1+2\*3)=7

I tried (found on google):

a.zip(b).map {|i,j| i\*j} (don’t really know how that works)

and how to add that to c array ?

Thanks

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [June 7, 2009, 5:24pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713/2 "2009-06-07T17:24:33Z")

</div>

On Sun, Jun 7, 2009 at 4:15 PM, Haris Bogdanoviæ  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to multply all elements of two arrays and add that to all elements of  
> the third array like this:
> 
> c+=a\*b
> 
> c=[1,1,1]  
> a=[2,2,2]  
> b=[3,3,3]
> 
> c=[7,7,7] # (1+2\*3)=7

How about a very simple each\_index approach?

> > c.each\_index { |i| c[i] += a[i] \* b[i] }  
> > =\> [7, 7, 7]

Hope that helps,  
Michael

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [June 7, 2009, 5:32pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713/3 "2009-06-07T17:32:11Z")

</div>

On Sun, Jun 7, 2009 at 10:15 AM, Haris  
Bogdanoviæ[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> c=[7,7,7] # (1+2\*3)=7
> 
> I tried (found on google):
> 
> a.zip(b).map {|i,j| i\*j} (don’t really know how that works)

zip weaves two enumerables together and creates a new enumerable:

a.zip(b) # =\> [[2, 3], [2, 3], [2, 3]]

[[2, 3], [2, 3], [2, 3]].map {|i,j| i\*j}

takes each sub-array, multiplies the two elements and returns a new  
array with the products which produces [6, 6, 6]

> and how to add that to c array ?

Just use zip and map again with a block which adds the elements.

c = c.zip(a.zip(b).map {|i, j| i\*j}).map {|i, j|i + j}

## Which changes the variable c to reference a new array which turns out to be [7, 7, 7]

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [June 7, 2009, 6:40pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713/4 "2009-06-07T18:40:50Z")

</div>

Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > a=[2,2,2]  
> > a.zip(b) # =\> [[2, 3], [2, 3], [2, 3]]  
> > Just use zip and map again with a block which adds the elements.
> 
> c = c.zip(a.zip(b).map {|i, j| i\*j}).map {|i, j|i + j}
> 
> Which changes the variable c to reference a new array which turns out  
> to be [7, 7, 7]

And if you’re going to be doing this kind of thing a lot, you might like  
to generalize the repeated zip → map operation:

class Array  
def combine(other, method)  
zip(other).map {|x,y| x.send(method,y)}  
end  
end

c = [1,1,1]  
a = [2,2,2]  
b = [3,3,3]

c = a.combine(b, :\*).combine(c, :+)  
#=\> [7,7,7]

🙂 I’m sure there’s a cleaner way… m.

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [June 7, 2009, 10:57pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713/5 "2009-06-07T22:57:24Z")

</div>

Haris Bogdanoviæ wrote:

> 

require ‘pp’  
d=[]  
a.each\_with\_index{|o, i| d[i] = a[i] + b[i] \* c[i] };  
pp d

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [June 7, 2009, 7:06pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713/6 "2009-06-07T19:06:46Z")

</div>

On 07.06.2009 17:31, Rick DeNatale wrote:

> On Sun, Jun 7, 2009 at 10:15 AM, Haris  
> BogdanoviÄ‡[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > a.zip(b).map {|i,j| i\*j} (don’t really know how that works)
> 
> zip weaves two enumerables together and creates a new enumerable:
> 
> a.zip(b) # =\> [[2, 3], [2, 3], [2, 3]]

This is true only if you do not provide a block:

irb(main):007:0\> a = Array.new 5 do |i| i end  
=\> [0, 1, 2, 3, 4]  
irb(main):008:0\> b = Array.new 5 do |i| i \* 2 end  
=\> [0, 2, 4, 6, 8]  
irb(main):009:0\> a.zip(b) {|x,y| printf “%d - %d\n”,x,y}  
0 - 0  
1 - 2  
2 - 4  
3 - 6  
4 - 8  
=\> nil

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Haris\_BogdanoviS\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/a8b319/32.png) [@Haris\_BogdanoviS\_S](https://www.ruby-forum.com/u/Haris_BogdanoviS_S)\
**Post date:** [June 7, 2009, 11:37pm UTC](https://www.ruby-forum.com/t/adding-multiplying-array/169713/7 "2009-06-07T23:37:14Z")

</div>

On Sun, Jun 7, 2009 at 10:55 PM, Rha7 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘pp’  
> d=[]  
> a.each\_with\_index{|o, i| d[i] = a[i] + b[i] \* c[i] };  
> pp d

Since you don’t use the ‘o’ block variable, you can also use  
Array#each\_index (which is what I used in my first reply to this  
thread).

c.each\_index { |i| c[i] += a[i] \* b[i] }
