# Calculation of means in Narray objects

**URL:** <https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917>\
**Category:** Ruby\
**Created:** [March 10, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917 "2010-03-10T12:35:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 10, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917/1 "2010-03-10T12:35:18Z")

</div>

I’m trying to perform some calculations on data held in NArray objects  
but it is not entirely clear to me how this ought to be done (I’ve  
looked here: [http://narray.rubyforge.org/SPEC.en](http://narray.rubyforge.org/SPEC.en)). The data consist of  
3-dimensional arrays, e.g. 10x10x100 elements. What I’d like to do is  
calculate the means for the data at each for each of the first two  
dimensions for positions of the third dimension. For example, 1,11,21…  
then 2,12,22…, 3,13,23… &c. The end result would be a 10x10x10 array  
where position [0,0,0] would contain the mean of the values in  
[0,0,0],[0,0,9],[0,0,19]…[0,0,99] of the original array.  
If this makes any sense to anyone I’d appreciate any suggestions.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 10, 2010, 2:23pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917/2 "2010-03-10T14:23:05Z")

</div>

If you mean [0,0,0],[0,0,10],[0,0,20]…[0,0,90], try  
narray.reshape(10,10,10,10).mean(3)

Milo T. wrote:

> I’m trying to perform some calculations on data held in NArray objects  
> but it is not entirely clear to me how this ought to be done (I’ve  
> looked here: [http://narray.rubyforge.org/SPEC.en](http://narray.rubyforge.org/SPEC.en)). The data consist of  
> 3-dimensional arrays, e.g. 10x10x100 elements. What I’d like to do is  
> calculate the means for the data at each for each of the first two  
> dimensions for positions of the third dimension. For example, 1,11,21…  
> then 2,12,22…, 3,13,23… &c. The end result would be a 10x10x10 array  
> where position [0,0,0] would contain the mean of the values in  
> [0,0,0],[0,0,9],[0,0,19]…[0,0,99] of the original array.  
> If this makes any sense to anyone I’d appreciate any suggestions.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 10, 2010, 2:32pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917/3 "2010-03-10T14:32:52Z")

</div>

Masahiro Tanaka wrote:

> If you mean [0,0,0],[0,0,10],[0,0,20]…[0,0,90], try  
> narray.reshape(10,10,10,10).mean(3)

Thanks. Would you mind pointing me in the direction of any documentation  
on that particular method, please? I presume that the (10,10,10,10)  
refers to the dimensions of the dimensions of the new array (the first  
three 10s) and the step size to iterate (the last 10) but I am not sure  
about the mean(3).

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 10, 2010, 3:00pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917/4 "2010-03-10T15:00:45Z")

</div>

> Thanks. Would you mind pointing me in the direction of any documentation  
> on that particular method, please?

[http://narray.rubyforge.org/SPEC.en](http://narray.rubyforge.org/SPEC.en)  
is the only documentation.

> I presume that the (10,10,10,10)  
> refers to the dimensions of the dimensions of the new array (the first  
> three 10s) and the step size to iterate (the last 10)

reshape changes the shape of NArray without changing internal  
arrangement.  
In this case, it changes the last dimension with 100 elements into  
2-dimensions with 10x10 elements.

> but I am not sure about the mean(3).

in SPEC.en:

Statistics  
self.mean(dim,…) Mean  
…  
note: \* If dimensions are specified, statistics are performed  
on those dimensions and the rest dimensions are kept.  
\* Range can be used.  
\* If dimension is not specified, statistics are performed  
for all the elements.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 10, 2010, 3:12pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917/5 "2010-03-10T15:12:49Z")

</div>

Masahiro Tanaka wrote:

> reshape changes the shape of NArray without changing internal  
> arrangement.  
> In this case, it changes the last dimension with 100 elements into  
> 2-dimensions with 10x10 elements.

Thanks again.  
Apologies for the nuisance, but I would like to ask one further question  
in order to assist in my understanding. If I have an array that’s  
96x73x1680 and would like to take the mean of every 12th element from  
the 3rd dimension, ending up with 96x73x12 (each point in the 3rd  
dimension is therefore the mean of 140 points from the original array),  
would it be correct to use:

new = narray.reshape!(96,73,12,140).mean(3)

?

> note: \* If dimensions are specified, statistics are performed  
> on those dimensions and the rest dimensions are kept.

Ah, I see.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 10, 2010, 3:38pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917/6 "2010-03-10T15:38:49Z")

</div>

Masahiro Tanaka wrote:

> Right.  
> Note that reshape! changes the shape of original narray without creating  
> new object.  
> reshape creates new object with new shape.

The “!” was a mistake, I meant to do this:  
new = narray.reshape(96,73,12,140).mean(3)

That seems to be working as expected - thanks again!

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 10, 2010, 3:34pm UTC](https://www.ruby-forum.com/t/calculation-of-means-in-narray-objects/184917/7 "2010-03-10T15:34:53Z")

</div>

Milo T. wrote:

> Apologies for the nuisance, but I would like to ask one further question  
> in order to assist in my understanding. If I have an array that’s  
> 96x73x1680 and would like to take the mean of every 12th element from  
> the 3rd dimension, ending up with 96x73x12 (each point in the 3rd  
> dimension is therefore the mean of 140 points from the original array),  
> would it be correct to use:
> 
> new = narray.reshape!(96,73,12,140).mean(3)
> 
> ?

Right.  
Note that reshape! changes the shape of original narray without creating  
new object.  
reshape creates new object with new shape.
