# Max position for loop

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;
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;
posmax[i]=m;
rr2=Array.slice(rr2,0,m);
}``````

Good day Carla,

is this

``````rr = newArray(264,272,275,276,276,276,282);
rr = Array.sort( rr );
rrX = newArray( rr.length );
rrX = rr;
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

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

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;
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
{"ft_lang_label":"__label__en","ft_lang_prob":0.73614764,"math_prob":0.98183286,"size":1178,"snap":"2020-45-2020-50","text_gpt3_token_len":344,"char_repetition_ratio":0.12606473,"word_repetition_ratio":0.0,"special_character_ratio":0.3132428,"punctuation_ratio":0.20618556,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9957782,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-11-29T04:27:55Z\",\"WARC-Record-ID\":\"<urn:uuid:60366196-6ce7-438c-bfcb-59a994895920>\",\"Content-Length\":\"21184\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:8f63541a-df9a-4fc9-9371-df7e0a8d646f>\",\"WARC-Concurrent-To\":\"<urn:uuid:1487960e-c438-4ff3-bbeb-480e36e6a1a1>\",\"WARC-IP-Address\":\"72.52.80.15\",\"WARC-Target-URI\":\"https://forum.image.sc/t/max-position-for-loop/6369\",\"WARC-Payload-Digest\":\"sha1:AQOYQV6W4V5AT4F23J5AJFA4DOVD335X\",\"WARC-Block-Digest\":\"sha1:JXSF63NQ4A4LBUCOHIIVDJJHIZ7EFDI6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-50/CC-MAIN-2020-50_segments_1606141196324.38_warc_CC-MAIN-20201129034021-20201129064021-00198.warc.gz\"}"}