Calculation of means in Narray objects

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

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

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

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

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.

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.

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!

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs