Sum of all elements in an array

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Sum of all elements in an array

William Nicolas
Hello ImageJ community,

I am reaching to you because it seems that there is no function or command implemented (as far as I know) that allows the summing up of all numerical elements contained in an array.

Let me explain my self with an example :

If I create an array :

        A=newArray(1,2,3);

I would like to make operations on the 3 elements for instance summing or multiplications (1+2+3 or 1*2*3 etc.)

Do you know if there is a command I missed or if a function doing this is available ?  thanks in advance !

Merry Xmas

William Nicolas

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Sum of all elements in an array

Gabriel Landini
On Monday 29 Dec 2014 11:58:23 you wrote:
> If I create an array :
> A=newArray(1,2,3);
> I would like to make operations on the 3 elements for instance summing or
> multiplications (1+2+3 or 1*2*3 etc.)

I would not be reasonable to expect every possible operation to have its own
function. Just use the macro language:

total=0;
A=newArray(1,2,3);
for (i=0;i<lengthOf(A);i++){
 total=total+A[i];
}
print (total);

or use
total=1; and  total=total*A[i];
for the multiplication of the array entries.

Cheers

Gabriel

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html