cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar

Using Lists and Matrices

Started ‎11-08-2022 by
Modified ‎11-08-2022 by
View Fullscreen Exit Fullscreen

So this demonstration is going to look at some of the basic features of lists and matrices. So I'm going to start with lists. And I'm going to get a new script editor window. So again, the keyboard shortcut is Ctrl+T. And I will resize and rearrange that. Let me go ahead and bring the log in front of the journal. And I'm going to create a variable I'll call my list. And I'll assign that the results of the list function. So the word list open parentheses, and I'm going to use the variables a, b. And then I'm going to calculate the square root of So that's sqrt and then in parentheses. And I'll go ahead and reformat just to get the capitalization and spacing in there. And I'll run the script. And so what shows up in the log is simply the information that a list with three elements has been assigned. If I hover over my list, what we see is that it is storing the items that I entered unevaluated. So it's the two variables and the function square root On a new line in my Script Editor window, I'm going to request evaluation of the items in my list. So I'll type eval list open parentheses my list. And I'll run just that line. And so there I see the evaluated results. Now, my list remains unevaluated. So if I wanted to work with the results of eval list, one thing would be to assign the result of evaluating the list to a new list. There are some other ways we can use subscripts, though, to work with evaluated items from the list. So before I talk about that, let me talk about subscripting the list just in general. So I'll go to a new line. And I'm going to type my list open square brackets. And I'll type a to extract the second item in the list. And if I run just that line, what I see is the unevaluated item. So what if I wanted the evaluated result? Well, I can actually come back up to my second line here. I can request evaluation of my list and then subscript that result. So I'll put open square bracket, a in there, close the square bracket. And I'll run just that line again, using the Enter key on the numeric keypad on the far right of my desktop computer keyboard. So there, I just subscripted the evaluated list to get that result back. We talked about modifying lists. So I want to go to a new line. And I'm going to use insert into. So insert into will modify the list in place. So I'll open parentheses. And what list am I modifying? Well, it's stored in my list. That's the first argument. What do I want to add? I'll add the name John. So that's a character string. And if I don't put that comma there and don't supply the third argument, it just puts it at the end of the list. But I'm going to have the comma, and I'm going to type a as that optional third argument to put it in the second position. And then, I'll run just that line. And insert into actually does not return anything to the log. But if I hover over my list anywhere it appears in the script, you can see that it's been modified to have the name John in the second position. Now, let's suppose I have a list, perhaps it's been returned to me by a JSL function or it's been used to collect user input, for example. And I need to find an item in the list so that I can remove it. I know it's-- well, I don't know it's in the list. I want to first query if it's in the list. And then if it is, I want to be able to remove it. So I'm going to create a variable to store the position of the item John. I'll call that pos. And I'll assign that the result of the contains function. And so we've seen contains with character strings. Now it contains with a list. We're looking in my list for the character string John, and it will be case sensitive, of course. And if I run that line, the result that's returned and stored in the variable pos is And so if I then wanted to remove that from the list, for example, I could go ahead and type remove from open parentheses. We're looking in my list. And I want to instead of just removing the last item, which is what I would get if I didn't supply anything after the first argument, I'm going to use the variable pos. And I run that line. And that returns a list with whatever number of elements are removed. By default we take one starting from the position specified in the second argument. So I get a list that has that character string. And I could actually assign that result to its own variable if I wanted to use that element of the list elsewhere in my script. And I could also extract it from the list it's returned in by subscripting this with a So let me -- right now, my list doesn't contain John anymore. I'm going to run my line again. The value stored in pos will still be the same. And I'm going to run line And so that returned the character string not in the list. And if I hover over my list, we can see that it's been returned to its previous value. Let's talk a little bit about matrices. I'm going to right click in the log to clear it and then select everything in the Script Editor window and delete that. So I'm going to start by simulating people, which will be my rows in my matrix. And each of those people will roll a die five times, a six-sided die. And that'll be my columns. And I'm going to just generate these with a random number generator, but I'm going to start with a specific seed value so that if you follow along, you'll get the exact same random numbers. And we're going to use a function called the j function, which makes a matrix with a specified number of rows and columns. Those are its first two arguments and then what will be the value in the matrix, that's the third argument. So I'm going to use the random reset function with a value of And, again, this is just specifying my starting seed for the random number generator. So I'll go ahead and run that. And then on a new line, I'll create a variable m, which will be assigned the result of the j function. Again, I start with the number of rows where we're simulating people and then the number of columns, rolling a six-sided die five times. And I'll use the random integer function with the arguments-- we'll start at and select random numbers up to And, again, I can reformat this just to make it look a little-- oops, I ran it instead of reformatting it. I can still reformat it. There we go. So it's also run now. And if we make the log a little bigger, there you can see that I have that row by column matrix. And how do I refer to elements in that matrix? Well, we use subscripts. So I'm going to go to a new line. Maybe I'm interested in what the fifth person rolled on their third roll of the die. So I'm going to subscript the variable name m, so m and then square brackets. for the row number, comma, for the column number, close the square bracket and run that line. And so the fifth person for their third roll rolled a And we can actually verify that. So there's one, two, three, four, there's the fifth row and there's the third roll of the die. Now, what if I want to find an entire row or an entire column? Well, on a new line here-- actually, I'll use the same line and just replace the subscript in my line Let's say I want all of row So I type for the first subscript. And then after the comma, I'm going to type a indicating that I don't want it to select any particular column. This is going to be row every column. And I can run that. And that gives me row all the values. And we could if you want to scroll up verify that those are indeed the values in row So that is, in fact, what we return. I could do the same thing to select a column where I would just have a for the row subscript and then whatever column number. But let's suppose I want to get rid of an entire row. Let's say I decide that the third person somehow cheated on their die rolling. So I'm going to subscript m with a and a And then before the semicolon, I'm going to assign that row the value of an empty matrix. So I just open and close the square brackets. And if I run that line, what's returned to the log is an empty matrix. And if I hover over m and you want to count the rows, you can see there are only nine rows now. Now, again, just kind of playing around with some of the things that we can do with matrices, let's suppose the nine remaining people each role the die for a sixth time. So I'm going to do this in place. The operators, I'll have m. And then the assignment operator will be the Concat So it's the double vertical bars and an equal sign. No spaces, because we can't have spaces in between the components of these operators that have multiple characters. And I'll use the j function again. And we only have nine rows. And I want one column. And I'm going to use random integer again. And for its arguments it's still a six-sided die, so I'll supply So this will modify m in place by adding a sixth column when I run that line. And so there you can see now we have six columns in the matrix. Let's clear the log. And I'm going to start over with assigning values to m. I'm not going to use random numbers anymore. So I'm going to clear the contents of the Script Editor window. I'm going to initialize a counter variable i and assign that the value of So I'll run that just to initialize that. And then on a new line, I'll assign a different matrix to m. So m will be assigned the result of the j function. It'll have five rows. This matrix will have five rows, five columns, and starting at will increment i by for every element of the matrix. So I'll use the i plus plus, that post increment operator that adds in place. And I'll go ahead and run that. And so I have a by matrix. And note that we are incrementing row-wise, not column-wise. And that's important for another method of subscripting that we're going to see coming up. So we increment row-wise. Now, what if I want a sequence that actually goes up to Well, there are certainly lots of different ways to do that. But a quick way would just be to multiply every element in the matrix I just created by And again, we can vectorize these operations. I don't have to loop through the matrix. I just assign -- And I'm going to do another in-place assignment-- m and then the asterisk equals, so the multiply to We don't have to do it in place. But rather than create a new variable, I'll do that. So again, I don't have to loop. It's going to go element-wise multiplying everything by And I run that. And so there's that matrix that goes from to in increments of Now, another way to create this matrix would be in two steps, starting with the index function. So I'm going to clear what's in the Script Editor window. I'll go ahead and clear the log as well. And in the Script Editor window, I'm going to type m. And that will be assigned the value that results from the index function-- index open parentheses. And we'll start at We'll count to The optional third argument, which I will use here is the increment. So we'll go by So here's the first step of creating that matrix that we had the by matrix. So we create a row vector using the index function. And then, I can shape this. So I'm going to go to a new line. I'm going to overwrite m again. So m will be the result of the shape function, which will take m as its first argument and change its dimensions to have five rows and five columns. You have to be very careful with this shape function. If your row vector in this case doesn't match up to a by square matrix that I've specified here, JMP will either truncate this vector if the matrix you specify with shape is too small. Or if you specify a bigger matrix, it'll just start repeating the vector. So be cautious when you use the shape function. So I want to run that. And there is that square matrix again, going from to in increments of Now, part of why I wanted to do that, aside from the fact that there's lots of ways to do everything in JMP usually, is to talk about another way of subscripting. We've seen the row by column subscripts. And I want to talk about the single subscript that you'll see sometimes. So if I wanted to-- we can use the contains function with-- we've seen it with character strings. You can use it with lists. We can use it with matrices. If I want to find the location of the number in the matrix that's currently stored m, well, if I was describing that I would say, OK, it's row column So I'd use a comma subscript. But if I asked JMP to find that for me-- so I'm going to just use the contains. And again, in practice, your script might assign this result to a variable. I don't need to do that here-- contains and then m comma JMP is not going to give me It's going to give me a single value. Well, how does that single value work? The single value subscript-- so if I came back-- I'm going to go to a new line here and type m and then the square brackets with a single value subscript and run that. That's The single value subscript is going to treat my matrix as though it were a row vector and start counting out from the first position. So when we go back in the log to see the result of my index function-- so that single subscript treats it as a row vector and counts row-wise, just like we incremented row-wise, to get the location of that value. And we'll see other examples when we work with matrices later on of JMP returning that single subscript to us. The last thing we'll do here is just kind of an arbitrary computation to illustrate that there are functions that can take matrices as their arguments, computational functions -- so we've already seen the shape function and contains. But I'm going to go ahead to a new line here and type log and then m as its argument divided by And the other thing about this example-- well, let me run it. And then I'll tell you the other thing about the example. So I've just done that computation on every element and divided by But the other thing I want to just mention is that in JSL, functions can take functions as arguments. We'll see that throughout. We've already seen some examples. So this divide operator, this is really the divide function taking the log of m as an argument and then And I did not need to compute the log of m and store it in a variable to supply it to the divide function. I'm able to do that computation directly. And again, what this would look like if I wrote the function out would be divide open parentheses. Replace the operator with a comma and close parentheses. So I don't have to supply a variable storing these values. I can compute them while I am running the divide function. So those are several examples of lists and matrices and some of the ways that we can work with those particular types of data structures.

Comments

Good presentation, thanks. 

Very helpful.