Tuesday, August 11, 2015

HP Prime: Picking Out Elements Using a Logical List (Updated 10/18/2015)

HP Prime:  Picking Out Elements Using a Logical List

I am taking an online class in R Programming language (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.

EXPORT BOOLLIST(LA, LB)
BEGIN

LOCAL LC, n, s, k, j;

//  Initialization
LC≔{ };
j≔1;
s≔SIZE(LA);
n≔SIZE(LB);

// Process
FOR k FROM 1 TO s DO

IF LB(j)==1 THEN
LC≔CONCAT(LC,LA(k));
END;

j≔j+1;

IF j>n THEN
j≔1;
END;

END;

RETURN LC;

END;


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}

See you next time, Eddie

Update:  There was an error in the program listing.  Previously I had b:=SIZE(LB); where it should be n:=SIZE(LB).  Eddie




This blog is property of Edward Shore – 2015.

TI 84 Plus CE: Consolidated Debts

TI 84 Plus CE: Consolidated Debts   Disclaimer: This blog is for informational and academic purposes only. Financial decisions are your ...