Hi there,
I'm working on a code that would enable me to count the number of elements that fulfil a particular type of criteria. Here's what I've but it seems that the array.concat function throws my code off if I've this code embedded in a loop. I.e. data from other channels are also concatenated. Can someone suggest an alternate method for me to make this work? In the e.g. below, I've set values that are equal or above 20 to be identified for counting. 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/scale); Many thanks. wj -- Sent from: http://imagej.1557.x6.nabble.com/ -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Good day wj,
I'm far from sure what you would like to achieve with your macro code but this X = Array.concat(X, a[i]); wont work! (X as an argument is not known to the function. It is not defined.) Please tell us what _exactly_ you expect as a result and BTW variable scale is not defined as well. Regards Herbie :::::::::::::::::::::::::::::::::::::: Am 04.09.17 um 04:35 schrieb wei.jian: > Hi there, > > I'm working on a code that would enable me to count the number of elements > that fulfil a particular type of criteria. Here's what I've but it seems > that the array.concat function throws my code off if I've this code embedded > in a loop. I.e. data from other channels are also concatenated. Can someone > suggest an alternate method for me to make this work? In the e.g. below, > I've set values that are equal or above 20 to be identified for counting. > > 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/scale); > > Many 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 |
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 |
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 |
Free forum by Nabble | Edit this page |