Subscribe Bookmark RSS Feed

joining matrices by matching columns

ron_horne

Super User

Joined:

Jun 23, 2011

Does anyone have a sleek script for joining matrices in the way joining tables does (by matching columns)?

Thanking you all in advance,

Ron

1 ACCEPTED SOLUTION

Accepted Solutions
ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Only slightly tongue in cheek do I suggest the following. It might seem perverse, but, on the other hand, getting JMP to do the work for you is always good. In this case it would take some effort to re-invent the flexibility of 'Join()' in the world of matrices.

NamesDefaultToHere(1);

// Joins two matrices 'm1' and 'm2' using the key columns 'c1' and 'c2' (all columns are retained)

joinMatrices =

Function({m1, c1, m2, c2}, {Default Local},

dt1 = AsTable(m1, << Invisible);

dt2 = AsTable(m2, << Invisible);

// Edit the 'Join()' expression to taste . . .

dt3 = dt1 << Join(

With( dt2 ),

By Matching Columns( "Col"||Char(c1) = "Col"||Char(c2) ),

Drop multiples( 0, 0 ),

Include Nonmatches( 0, 0 ),

Preserve main table order( 1 ),

Invisible

);

m3 = dt3 << getAsMatrix;

Close(dt1, NoSave);

Close(dt2, NoSave);

Close(dt3, NoSave);

m3;

);

// Make a couple of matrices

n = 10; // Number of rows

m1 = (1::n)`||J(n, n, 10);

m2 = RandomShuffle(m1[0,1])||Identity(n);

// Test the function

joinMatrices(m1, 1, m2, 1);

7 REPLIES
ian_jmp

Staff

Joined:

Jun 23, 2011

Solution

Only slightly tongue in cheek do I suggest the following. It might seem perverse, but, on the other hand, getting JMP to do the work for you is always good. In this case it would take some effort to re-invent the flexibility of 'Join()' in the world of matrices.

NamesDefaultToHere(1);

// Joins two matrices 'm1' and 'm2' using the key columns 'c1' and 'c2' (all columns are retained)

joinMatrices =

Function({m1, c1, m2, c2}, {Default Local},

dt1 = AsTable(m1, << Invisible);

dt2 = AsTable(m2, << Invisible);

// Edit the 'Join()' expression to taste . . .

dt3 = dt1 << Join(

With( dt2 ),

By Matching Columns( "Col"||Char(c1) = "Col"||Char(c2) ),

Drop multiples( 0, 0 ),

Include Nonmatches( 0, 0 ),

Preserve main table order( 1 ),

Invisible

);

m3 = dt3 << getAsMatrix;

Close(dt1, NoSave);

Close(dt2, NoSave);

Close(dt3, NoSave);

m3;

);

// Make a couple of matrices

n = 10; // Number of rows

m1 = (1::n)`||J(n, n, 10);

m2 = RandomShuffle(m1[0,1])||Identity(n);

// Test the function

joinMatrices(m1, 1, m2, 1);

ron_horne

Super User

Joined:

Jun 23, 2011

thanks Ian@JMP​ for the script and the tip.

I agree that the power of JMP is outstanding and much easier to work with than scripting crude commands using matrices. in my case it also saves a huge amount of time since i can quickly make "hands on" checks that it is doing what i intended by using the interactive interface at any step.

the thing is the script i wrote in which all the process uses data tables - summarizing, joining, comparing, deleting rows and extracting values. i was hoping to make it faster by converting it to matrices but i notice this will not be the case.

Ron

ian_jmp

Staff

Joined:

Jun 23, 2011

Sorry Ron. My answer was not to imply that one couldn't make specific cases of 'joining' faster with matrix manipulations - Probably one could. It's just that (as the options in 'Tables > Join' show) joining is not 'one thing', and I didn't know the details of your case(s).

msharp

Super User

Joined:

Jul 28, 2015

From my knowledge, joins and a few other table operations are the only multi-threaded operations in JMP.  I would put all my money that a table join would be faster than a matrix join.

ron_horne

Super User

Joined:

Jun 23, 2011

thanks msharp, this is useful to know.

what about table summary? deleting multiple rows? deleting multiple columns?

Ron

msharp

Super User

Joined:

Jul 28, 2015

Yes, all those functions should be multithreaded. 

Multithreading largely isn't supported in JSL (this is for the most part a good thing).  However, if that were true for JMP itself the program would be super slow and unusable to an end user.  Most send commands '<<' tend to be multithreaded b/c they are native to JMP, although not all.

Just test out a function if you have a question on it. For example, open up your Task Manager and run something like this:

dt = new table("test");

dt << New Column("test", Numeric, Continuous, Values(1::100000000));

dt << Delete Rows(1::100000000);

clost(dt, nosave);

When I run this you can see all four of my processors spiking.

10285_pastedImage_2.png

ron_horne

Super User

Joined:

Jun 23, 2011

thanks!

so i guess i am back to my old table commands after all....