Picking Out Elements Using a Logical List - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: HP Software Libraries (/forum-10.html) +--- Forum: HP Prime Software Library (/forum-15.html) +--- Thread: Picking Out Elements Using a Logical List (/thread-4513.html) |
Picking Out Elements Using a Logical List - Eddie W. Shore - 08-12-2015 03:05 AM I am taking an online class in R Programming language (http://www.edx.org), having a great time. The program BOOLLIST is based on the ability in R to pick out elements using logical elements (TRUE, FALSE). Example (in R): vector <- [2, 3, 4, 5] vector[ c(TRUE, TRUE, FALSE, TRUE) ] returns [2, 3, 5] The logical vector doesn’t have to be same length as the source vector. If the logical vector has elements than the source vector. vector <- [1, 2, 3, 4, 5, 6] vector[ c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE) ] returns [1, 3, 5] vector[ c(TRUE, FALSE) ] returns [1, 3, 5] (TRUE, FALSE pattern recycles) Program BOOLLIST: Input: BOOLLIST(source list, logical list) Notes: Use list brackets { }. For the logical list, use 1 for TRUE and 0 for FALSE. Code: EXPORT BOOLLIST(LA, LB) Examples: In addition to the examples above that can be tried with BOOLLIST: BOOLLIST( {4,2,3,6}, {1,0,0,1} ) returns {4, 6} BOOLLIST( {3,9,6,-1,6}, {1,0} ) returns {3, 6, 6} http://edspi31415.blogspot.com/2015/08/hp-prime-picking-out-elements-using.html RE: Picking Out Elements Using a Logical List - Didier Lachieze - 08-12-2015 10:40 AM Nice function ! I was expecting it would be very easy to implement it with the current Prime list functions but I've not found a simple solution without a FOR loop. So, here is another way to do it: Code: EXPORT BOOLLIST(LA,LB) Note: storing to LC(0) is used to add an element to the list LC. RE: Picking Out Elements Using a Logical List - roadrunner - 08-16-2015 11:53 AM Here is a method without a for loop: Code:
Don't know if it qualifies as simple or not; it runs substantially slower than your version. RE: Picking Out Elements Using a Logical List - roadrunner - 08-16-2015 11:40 PM And another version using no declared variables and no for loop: Code: export BOOLLIST4(LA,LB) RE: Picking Out Elements Using a Logical List - Didier Lachieze - 08-17-2015 10:56 AM Nice idea to use the recursion to solve this problem. Here is a variation of your first program: Code: EXPORT BOOLLIST5(LA,LB) RE: Picking Out Elements Using a Logical List - Thomas Klemm - 08-17-2015 11:32 AM (08-17-2015 10:56 AM)Didier Lachieze Wrote: Nice idea to use the recursion to solve this problem. RE: Picking Out Elements Using a Logical List - roadrunner - 08-18-2015 12:13 PM (08-17-2015 10:56 AM)Didier Lachieze Wrote: Here is a variation of your first programI was not aware of the tail function (nor the head function). Thank you for sharing. |