Dear all,
I would have a question that is not specifically ImageJ related, but rather with programming in general. Indeed, please consider the following macro code: boola = newArray(true, false, true, true, false); for(i = 0; i != boola.length; i++) if(boola[i]) print(boola[i]); Would there be a way to combine the for and if loops within a single one giving the same result than higher? I thank you very much in advance about your lighting concerning this question. My best regards, Philippe Philippe CARL Laboratoire de Biophotonique et Pharmacologie UMR 7213 CNRS - Université de Strasbourg Faculté de Pharmacie 74 route du Rhin 67401 ILLKIRCH Tel : +33(0)3 68 85 41 84 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Dear Philippe,
I am unsure on what you mean by 'if loop', there is only one loop in your code, the 'for' loop. As far as I use, Matlab is capable of doing something like that where accessing a vector's indexes with a Boolean vector allows you to only work on the 'true' elements. ----- Matlab example: k = round(rand(10,1)*10); disp(k(k>5)) example output of the last command: 10 10 8 9 8 10 ----- Matlab example ends In the macro language, you need to loop (with the 'for') and check if a given condition is met (with the 'if') as you did in your example. There is no shorthand notation either, unfortunately. All the best, Oli -----Original Message----- From: ImageJ Interest Group [mailto:[hidden email]] On Behalf Of Philippe CARL Sent: lundi, 2 mai 2016 18:40 To: [hidden email] Subject: Algorithmic issue Dear all, I would have a question that is not specifically ImageJ related, but rather with programming in general. Indeed, please consider the following macro code: boola = newArray(true, false, true, true, false); for(i = 0; i != boola.length; i++) if(boola[i]) print(boola[i]); Would there be a way to combine the for and if loops within a single one giving the same result than higher? I thank you very much in advance about your lighting concerning this question. My best regards, Philippe Philippe CARL Laboratoire de Biophotonique et Pharmacologie UMR 7213 CNRS - Université de Strasbourg Faculté de Pharmacie 74 route du Rhin 67401 ILLKIRCH Tel : +33(0)3 68 85 41 84 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
In reply to this post by CARL Philippe (LBP)
Hi Philippe,
If you use one of the scripting languages instead of the macro language, there are many ways with varying levels of elegance and obfuscation, depending on your taste. For example, here is an SO post discussing this issue for Python: http://stackoverflow.com/q/6981717/1207769 And here is some awesome stuff with Groovy: http://naleid.com/blog/2008/12/01/groovy-makes-iteration-easy My personal taste regarding for+if is to eliminate the extra level of indentation by using the "continue" keyword (or equivalent in whatever language you are using), as in: boola = newArray(true, false, true, true, false); for(i = 0; i != boola.length; i++) { if (!boola[i]) continue; print(boola[i]); } Regards, Curtis -- Curtis Rueden LOCI software architect - http://loci.wisc.edu/software ImageJ2 lead, Fiji maintainer - http://imagej.net/User:Rueden Did you know ImageJ has a forum? http://forum.imagej.net/ On Mon, May 2, 2016 at 11:39 AM, Philippe CARL <[hidden email]> wrote: > Dear all, > > I would have a question that is not specifically ImageJ related, but rather > with programming in general. > > Indeed, please consider the following macro code: > > boola = newArray(true, false, true, true, false); > > for(i = 0; i != boola.length; i++) > > if(boola[i]) > > print(boola[i]); > > Would there be a way to combine the for and if loops within a single one > giving the same result than higher? > > I thank you very much in advance about your lighting concerning this > question. > > My best regards, > > Philippe > > > > Philippe CARL > > Laboratoire de Biophotonique et Pharmacologie > > UMR 7213 CNRS - Université de Strasbourg > > Faculté de Pharmacie > > 74 route du Rhin > > 67401 ILLKIRCH > > Tel : +33(0)3 68 85 41 84 > > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Dear Curtis,
I thank you very much for having pefectly understand what I was looking for, as well as giving me the solution. In fact I wanted to eliminate one (of the two) levels of indentation needed by the code I gave as example. Indeed, I have a huge macro calling several plugins where I want to increase the complexity at several places within the code. And I was convinced this issue could be solved with the addition of only one indentation level, but unfortunatelly I wasn't able to figure out how. Thus I thought again about the words of Feyman who said: "There is always an easy way to explain everything, it is just very hard to find it". And now you gave me the solution, it was indeed very easy. But without your help, it was very very hard!!! So thanks a lot four your answer and help. Kindest regards, Philippe Le Lundi 2 Mai 2016 19:12 CEST, Curtis Rueden <[hidden email]> a écrit: > Hi Philippe, > > If you use one of the scripting languages instead of the macro language, > there are many ways with varying levels of elegance and obfuscation, > depending on your taste. > > For example, here is an SO post discussing this issue for Python: > http://stackoverflow.com/q/6981717/1207769 > > And here is some awesome stuff with Groovy: > http://naleid.com/blog/2008/12/01/groovy-makes-iteration-easy > > My personal taste regarding for+if is to eliminate the extra level of > indentation by using the "continue" keyword (or equivalent in whatever > language you are using), as in: > > boola = newArray(true, false, true, true, false); > for(i = 0; i != boola.length; i++) { > if (!boola[i]) continue; > print(boola[i]); > } > > Regards, > Curtis > > -- > Curtis Rueden > LOCI software architect - http://loci.wisc.edu/software > ImageJ2 lead, Fiji maintainer - http://imagej.net/User:Rueden > Did you know ImageJ has a forum? http://forum.imagej.net/ > > > On Mon, May 2, 2016 at 11:39 AM, Philippe CARL <[hidden email]> > wrote: > > > Dear all, > > > > I would have a question that is not specifically ImageJ related, but rather > > with programming in general. > > > > Indeed, please consider the following macro code: > > > > boola = newArray(true, false, true, true, false); > > > > for(i = 0; i != boola.length; i++) > > > > if(boola[i]) > > > > print(boola[i]); > > > > Would there be a way to combine the for and if loops within a single one > > giving the same result than higher? > > > > I thank you very much in advance about your lighting concerning this > > question. > > > > My best regards, > > > > Philippe > > > > > > > > Philippe CARL > > > > Laboratoire de Biophotonique et Pharmacologie > > > > UMR 7213 CNRS - Université de Strasbourg > > > > Faculté de Pharmacie > > > > 74 route du Rhin > > > > 67401 ILLKIRCH > > > > Tel : +33(0)3 68 85 41 84 > > > > > > -- > > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > > > > -- > 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 |