Login  Register

Re: Counting the number of elements in an array based on a set criteria

Posted by Herbie on Sep 04, 2017; 3:17pm
URL: http://imagej.273.s1.nabble.com/Counting-the-number-of-elements-in-an-array-based-on-a-set-criteria-tp5019312p5019317.html

Well wj,

perhaps this is what you are looking for:

///////////////////////////////////////////////////
a = newArray( 10, 20, 30, 40, 50, 40, 30, 20, 10 );
Y = newArray(a.length);
j = 0;
for (i=0; i<a.length; i++) {
        if  (a[i]>=20 ) {
                Y[j] = a[i];
                j++;
        }
}
Y = Array.trim( Y,  j );
Array.show( "Results", Y);
///////////////////////////////////////////////////

Regards

Herbie

:::::::::::::::::::::::::::::::::::::
Am 04.09.17 um 16:57 schrieb wei.jian:

> Hi Herbie,
>
> Thanks for your reply. This was written as a function so you are right about
> the 'scale' in the code. Below is the updated one. X as an argument serves
> as a filler for me to pick out values that are equal or greater than 20. In
> this eg. X is (0, 20, 30, 40, 50, 40, 30, 20). Next, I used the Array.slice
> function to remove the filler. Thus Y gives (20, 30, 40, 50, 40, 30, 20) and
> Y.length gives 7 as there are 7 elements based on my criteria.
>
> a = newArray(10, 20, 30, 40, 50, 40, 30, 20, 10);
>  
> for (i=0; i<a.length; i++) {
>   if (a[i]>=20) {
>   X = Array.concat(X, a[i]);
>   Y = Array.slice(X, 1, X.length);
>   }
>   }
>   Array.show("Results", Y);
>   print(Y.length);
>
> I understand that my approach in isolating elements is clunky so are there
> alternatives that I can use to pick out elements based on my criteria?
>
> Thanks.
> wj
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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