# Sum column table

I have a table with different value as diameter and volume.
Firstly, I would like to sum the different value of volume. I don t know if the function “sum” works ??

Secondly, I would like the values are sorted in ascending order

Then I whish to take 10% of the sum of value and found on table which diameter could correspond to 10% of volume ??

I just know the function searchbinary
Thank you

Code :

``````run("Clear Results");
run("Make Binary");
run("Set Measurements...", "area decimal=4");
run("Analyze Particles...", "display clear");
run("Distribution...", "parameter=Area automatic");
selectWindow("Area Distribution");
for(i=0; i<nResults; i++) {
area = getResult("Area", i);
setResult("d  [um]", i, 1000*1.06*pow(area,0.455));
}
for(i=0; i<nResults; i++) {
d = getResult("d  [um]", i);
setResult("Volume [ul]", i, PI/6*0.000000001*pow(d,3));
}

selectWindow("Area Distribution");
run("Close");
``````

Results

``````        Area          d             Volume
1	0.3873	688.4131	0.1708
2	6.400E-5	13.0954	1.176E-6
3	0.0002	21.5878	5.268E-6
4	0.0001	17.9509	3.029E-6
5	0.0855	346.2260	0.0217
6	0.0504	272.2934	0.0106
``````

Hi @tom51,

You could try a for loop that goes through all the volumes and adds it to the previous total. E.g.

``````    for(i=0; i<nResults; i++) {
totalVol += getResult("Volume", i);
}
``````

You could try using an array and sorting it. E.g.

``````    volumeArray = newArray(nResults);
for(i=0; i<nResults; i++) {
volumeArray[i+1] = getResult("Volume", i);
}
Array.sort(volumeArray);
``````

You can work out 10% of the sum using some math, and then cycle through the results table using a `for` loop and `getResult` for each volume value to find the result that corresponds to ~10%. When this is found you can retrieve the index (row) containing that value.

Best,

Rob

2 Likes

Hi, Ok thank you

I have thought to do this but it doesn t work

``````for (i = 0; i <= nResults && getResult+getResult("Volume", i) < 0.1*totalVol; i++) {q=index};

diameter = getResult( "d  [um]",q)
``````

What do you think ?

I would usually expand my scripts away from one line to make it easier to read. Maybe the problem with your script is `q = index` should be `q=i`?

Otherwise, i have made an edit to it below:

``````volumes = newArray(nResults);

for (i=0; i < nResults; i++) {
currentVol = getResult("Volume", i);
if (currentVol < 0.1*totalVol) {
volumes[i] = currentVol;
}
}

Array.getStatistics(volumes, min, max, mean, stdev);

for (i=0; i < nResults; i++) {
currentVol = getResult("Volume", i);
if (currentVol == max) {
row = i;
}
}

diameter = getResult("d", row);
``````

As an aside, please make your responses as clear as possible. Try quoting the parts of the post you are replying to as well, so the reply is less ambiguous.

Best,

Rob

EDIT: Made a slight edit to find the closest one to 10% of the total volume.

2 Likes

Hi,
Sorry, I give you more explications

This is the sum of volume which is 10 % and I woul like to know the last diameter.
I think you macro give the diameter of 10% of volume and no the sum ?

Thank you,

Hi @tom51,

You are correct that the script I wrote only gives the diameter from the row that contains the volume value that is closest to 10% of the sum of the volumes (although I haven’t tested it).

Okay, so to clear this up, you want:

1. To sort the volumes in to ascending order
2. Get the sum of the volumes
3. Get the cumulative sum of volumes down the rows until the cumulative sum is > 10% of the total sum
4. Find that row number
5. Find the diameter from that row number

Essentially I think you want the count number that results in 10% of a cumulative histogram, then find the row corresponding to that count. I think the tough part is if your values are not unique in the volume array, then retrieving the index from the original results table is difficult, but I haven’t thought about it too much.

I will have another go at some point, but someone might answer it before me.

Best,

Rob

Hi,

yes it s clear

Thank you

Hi,

I have found the program

``````V01=0.1*sum;
row=0;
dv01=0;
volumes = newArray(nResults);
currentVol =0;
for (row=0; currentVol + getResult("Volume [uL]", row) < V01; row++) {
currentVol = currentVol + getResult("Volume [uL]", row);
dv01 = getResult("d  [um]", row);
};
``````

Thank you

1 Like

It’s difficult to understand what you want exactly and how the results table is obtained. But, from what I understood, you wanted to sort the volume values, then find the diameter in the row with volume value that is the last value where the cumulative sum is <=10% of the total sum of volumes.

Is your results table sorted in ascending order? The example results table from earlier had duplicate values, no unique ones, I presume the real results table has multiple different values. If so, I’m wondering if you need to sort the results table according to ascending volume. But again, I have no idea where the values come from, or how you want to measure it exactly.

Everything is probably fine, but just wanted to mention it.

Best,

Rob

3 Likes