Login  Register

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

Posted by wei.jian on Sep 04, 2017; 2:57pm
URL: http://imagej.273.s1.nabble.com/Counting-the-number-of-elements-in-an-array-based-on-a-set-criteria-tp5019312p5019316.html

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