Max position for loop

plugin
fiji
macro
Tags: #<Tag:0x00007fd5464b14a0> #<Tag:0x00007fd5464b1220> #<Tag:0x00007fd5464b0d20>

#1

Hello I have an Array of value sorted in ascending order. My plan is to find the maximum of the array, and the position of the maximum value and cut the array from the beginning to the max position.This should be done in a for loop and the end I would like to have an array with all maximum values and the associated positions. As the Array.findMaxima function will find the first position at maximum value (so if I have 275,276,276… it will give me position number 1), I have created another array r3 without repeated value to understand how many iterations I need. This is what I have done so far. Of course it doesn’t work, there is something wrong in the for loop. Please, can you help me?

     rr=newArray(264,272,275,276,276,276,282);
     rr2=Array.sort(rr);
          b=0; // to have an array without repeated value
        rr2[b]=rr2[0];
        for(i=0;i<rr2.length;i++)
        {
            if (rr2[b]!=rr2[i])
            {
                b++;
                rr2[b]=rr2[i];
            }
        }
        r3=Array.slice(rr2,0,b+1);
       posmax=newArray(r3.length);
       arrmax=newArray(r3.length);
       rr2=Array.sort(rr);
        
        for (i=0;i<r3.length;i++){
        Array.getStatistics(rr2, min, max, mean, stdDev);
        arrmax[i]=max;
        maxLocs2= Array.findMaxima(rr2,0);
        m=maxLocs2[0];
        posmax[i]=m;
        rr2=Array.slice(rr2,0,m);
        }

#2

Good day Carla,

is this

rr = newArray(264,272,275,276,276,276,282);
rr = Array.sort( rr );
rrX = newArray( rr.length );
rrX[0] = rr[0];
j = 1;
for( i=1;i<rr.length;i++ ) {
	if ( rr[i-1] != rr[i] ) {
		rrX[j] = rr[i];
		j++;
	}
}
rrX = Array.trim( rrX, j );
Array.show( rrX );

what you want?

If yes, I guess your formulation of the problem prevented you from finding a solution …

Regards

Herbie


#3

Hi Herbie, thank you for your response.
My problem is in the last loop for. My idea in the loop for is to have the position of the first maximum (282) which is 7, then I to cut the array rr2 removing the maximum value (using the position in array slice) , so the new array should be in the second iteration ( 264,272,275,276,276,276); then I will calculate again the maximum position which will be 3, ecc.
So at the end I should have an array of position equal to (7,3,2,1,0).


#4

Well Carla,

then let’s try again:

rr = newArray( 264,272,275,276,276,276,282 );
rr = Array.sort( rr );
max = newArray( rr.length );
max[0] = 0;
j = 1;
for( i=1; i<rr.length; i++ ) {
	if ( rr[i-1] != rr[i] ) {
		max[j] = i;
		j++;
	}
}
max = Array.trim( max, j );
Array.reverse( max );
Array.show( max );

I hope you don’t insist on the result (7,3,2,1,0).

HTH

Herbie