Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar
XanGregg
Staff
JSL Tip: Use Matrices Instead of Lists

The list is one of the basic building tools for organizing data in JSL. The power of lists is that they can contain any kind of items (numbers, characters, other lists, ...), and they can grow and shrink as needed. However, if you just need a fixed list of numbers, you can get much better performance from a one-dimensional matrix.


Consider these two samples that make arrays of odd numbers.


// make a list of odd numbers

n = 1000;

y = {};

for (i = 1, i <= n, i++,

y = 2 * i - 1;

);

y;




// make a matrix of odd numbers

n = 1000;

y = J(1, n); // creates a 1 x n matrix with all values set to 1

for (i = 1, i <= n, i++,

y = 2 * i - 1;

);


For a few hundred items, there's not much difference. But as n goes beyond 1000 (or if the above code is in a loop), you start to notice the difference in speed. For lists, there is a certain amount of overhead for growing dynamically and general flexibility.


Notice the J function can be used to create a new matrix of a given size. Jm,n is the name for the Unit Matrix in linear algebra.


Also notice that a one-dimensional matrix can be indexed with just one subscript. That is, we can use y instead of y[1]. That makes it easier to treat matrices as lists, and in the loop above, which is the same in both cases.


Question until next time: How can the code be made even faster?

Article Labels

    There are no labels assigned to this post.

9 Comments
Visitor

Xan Gregg wrote:

The As List() function will convert a matrix to a list of lists, one for each row. If you want one long list, you can use the Shape() function to first put all the matrix elements into one row.

Visitor

Chris Dorger wrote:

How do I make a list out of a matrix or a data table column?

Visitor

Xan Gregg wrote:

JSL is the JMP Scripting Language. It's not needed for normal interactive use of JMP, but can be useful for custom tasks. File > New > Script will give you an empty script window where you can put scripts like the above snippets and run them with Edit > Run Script (Ctrl-R). The output, if any, goes to the Log window.

Visitor

Ryan Kelly wrote:

how do you run this in JMP?

Visitor

Xan Gregg wrote:

You're right. I thought I was being clever to save that improvement for a future tip, but maybe I should have found an example that couldn't be undermined.

Visitor

Michael wrote:

I would think that y=index(1,1000,2) would be faster though I haven't verified it.

Visitor

JSL Tip: Replace Loops with Functions on Matrices - JMP Blog wrote:

[...] my previous JSL tip, we saw how it is faster to access matrix items than to list items. It's faster still if you don't [...]

Visitor

Uday wrote:

Hello Mr.Gregg ,

So lets say I have this matrix with 5 columns and n rows . Now I want to modify these columns with a condition . For Eg: I want to make all the values NULL in each of the columns of the matrix which are less than 5 .

How would I also use these columns in a graph builder.

Regards

Uday

Staff

Xan Gregg wrote:

Hi Uday,

You should try posting in the forums at community.jmp.com where you can provide a more detailed description and get answers from anyone.

From your description here, I think the Loc() function will be useful to investigate. It will give you the locations of rows that meet a certain condition for subsequent operations.

To bring the columns into Graph Builder, make them into a table with, for instance, As Table().