cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
%3CLINGO-SUB%20id%3D%22lingo-sub-48496%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EConvert%20Matrix%20to%20Data%20Table%20and%20Back%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-48496%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3E%0A%3CH2%20id%3D%22toc-hId--1330332819%22%20id%3D%22toc-hId-1617152513%22%20id%3D%22toc-hId-1617152513%22%3EProblem%3C%2FH2%3E%0A%3CP%3EYou%20have%20data%20in%20a%20Matrix%20and%20need%20a%20Data%20Table%2C%20or%20the%20other%20way%20'round%3C%2FP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Data%20table%20or%20matrix%3F%22%20style%3D%22width%3A%20352px%3B%22%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Data%20table%20or%20matrix%3F%22%20style%3D%22width%3A%20352px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22Data%20table%20or%20matrix%3F%22%20style%3D%22width%3A%20352px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F8644i155E43B13460EE2E%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22matrix.PNG%22%20alt%3D%22Data%20table%20or%20matrix%3F%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EData%20table%20or%20matrix%3F%3C%2Fspan%3E%3C%2Fspan%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EData%20table%20or%20matrix%3F%3C%2FSPAN%3E%3C%2FSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EData%20table%20or%20matrix%3F%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%0A%3CH2%20id%3D%22toc-hId--442829138%22%20id%3D%22toc-hId--935004448%22%20id%3D%22toc-hId--935004448%22%3ESolution%3C%2FH2%3E%0A%3CP%3EThere%20is%20a%20function%2C%20AsTable()%2C%20that%20makes%20a%20new%20data%20table%20from%20a%20matrix%2C%20and%20a%20data%20table%20method%2C%20GetAsMatrix()%2C%20that%20makes%20a%20matrix%20from%20the%20numeric%20columns%20in%20a%20table.%3C%2FP%3E%0A%3CP%3EThere%20is%20also%20a%20way%20to%20index%20data%20tables%20directly%20with%20%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FData-table-subscripting%2Fba-p%2F21013%22%20target%3D%22_blank%22%3Edata%20table%20subscripting%3C%2FA%3E.%3C%2FP%3E%3CBR%20%2F%3E%3CPRE%3E%2F%2F%20make%20a%20matrix%20of%205%20rows%2C%203%20cols%2C%20random%20values%0Amat%20%3D%20J(%205%2C%203%2C%20Random%20Normal()%20)%3B%0A%2F%2F%20make%20a%20data%20table%20from%20a%20matrix%0Adt%20%3D%20As%20Table(%20mat%20)%3B%0A%2F%2F%20make%20a%20matrix%20from%20a%20data%20table%0Amat2%20%3D%20dt%20%26lt%3B%26lt%3B%20GetAsMatrix%3B%0A%2F%2F%20comparing%20two%20matrices%20returns%20a%20matrix%20of%20answers%0A%2F%2F%20and%20the%20All%20function%20returns%20true%20if%20all%20of%20the%0A%2F%2F%20matrix%20values%20are%20true%0AShow(%20All(%20mat%20%3D%3D%20mat2%20)%20)%3B%0A%2F%2F%20use%20data%20table%20subscripting%20to%20get%20a%20matrix%20of%20all%0A%2F%2F%20rows%20and%20all%20columns%20from%20the%20table%3B%20the%20zero%20subscript%0A%2F%2F%20is%20used%20with%20matrix%20and%20data%20table%20to%20mean%20%22all%0A%2F%2F%20rows%20or%20all%20columns%22%0Amat3%20%3D%20dt%5B0%2C%200%5D%3B%0AShow(%20All(%20mat%20%3D%3D%20mat3%20)%20)%3B%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-444674543%22%20id%3D%22toc-hId-807805887%22%20id%3D%22toc-hId-807805887%22%3EDiscussion%3C%2FH2%3E%0A%3CP%3E%26lt%3B%3CGETASMATRIX%20can%3D%22%22%20also%3D%22%22%20specify%3D%22%22%20the%3D%22%22%20columns%3D%22%22%20several%3D%22%22%20ways%3D%22%22%3E%3CBR%20%2F%3E%3C%2FGETASMATRIX%3E%3C%2FP%3E%3CH2%20id%3D%22toc-hId-1332178224%22%20id%3D%22toc-hId--1744351074%22%20id%3D%22toc-hId--1744351074%22%3ESee%20Also%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2FMatrices.shtml%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fwww.jmp.com%2Fsupport%2Fhelp%2FMatrices.shtml%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FWhat-is-the-JSL-Matrix%2Fba-p%2F33349%22%20target%3D%22_blank%22%3Ehttps%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FWhat-is-the-JSL-Matrix%2Fba-p%2F33349%3C%2FA%3E%3C%2FP%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-365095%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Convert%20Matrix%20to%20Data%20Table%20and%20Back%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-365095%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThanks!%3C%2FP%3E%3CP%3E%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FData-table-subscripting%2Fm-p%2F21013%23U21013%22%20class%3D%22lia-mention-container-editor-message%20lia-img-icon-blog-thread%20lia-fa-icon%20lia-fa-blog%20lia-fa-thread%20lia-fa%22%20target%3D%22_blank%22%3EData%20table%20subscripting%3C%2FA%3E%26nbsp%3Bdescribes%20indexing%20a%20data%20table%20as%20a%20matrix%2C%20and%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.jmp.com%2Ft5%2FUncharted%2FWhat-is-the-JSL-Matrix%2Fm-p%2F33349%23U33349%22%20class%3D%22lia-mention-container-editor-message%20lia-img-icon-blog-thread%20lia-fa-icon%20lia-fa-blog%20lia-fa-thread%20lia-fa%22%20target%3D%22_blank%22%3EWhat%20is%20the%20JSL%20Matrix%3F%3C%2FA%3E%26nbsp%3Bshows%20how%20to%20use%20matrix%20subscripting.%20So%2C%20with%20a%20data%20table%20(or%20a%20matrix)%2C%20rather%20than%20copying%20the%20data%20out%20of%20the%20table%20to%20make%20a%20matrix%2C%20you%20can%20get%20a%20row%20from%20either%20one%20like%20this%3A%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%2F%2F%20if%20dt%20is%20a%20table...%0ArowVector%20%3D%20dt%5B%20rowNumber%2C%200%20%5D%3B%20%2F%2F%20all%20columns%0ArowVector%20%3D%20dt%5B%20rowNumber%2C%20%7B%20age%2C%20height%2C%20weight%20%7D%20%5D%3B%20%2F%2F%20selected%20columns%0A%2F%2F%20if%20m%20is%20a%20matrix%0ArowVector%20%3D%20m%5B%20row%2C%200%20%5D%3B%0ArowVector%20%3D%20m%5B%20row%2C%205%3A%3A9%20%5D%3B%20%2F%2F%20selected%20items%20from%20row%0A%2F%2F%20and%20both%20dimensions%20at%20once%0Apatch%20%3D%20m%5B%203%3A%3A5%2C%207%3A%3A11%20%5D%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EThe%20zero%20subscript%20means%20%22all%20of%20this%20dimension%22.%20The%20matrix%20indexing%20starts%20at%20one.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-365060%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Convert%20Matrix%20to%20Data%20Table%20and%20Back%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-365060%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThanks%2C%20very%20useful%20%2C%20and%20any%20convient%20way%20to%20get%20a%20row%20vector%20from%20a%20data%20table%3F%3C%2FP%3E%3CP%3EThanks!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
Convert Matrix to Data Table and Back

Problem

You have data in a Matrix and need a Data Table, or the other way 'round

Data table or matrix?Data table or matrix?

Solution

There is a function, AsTable(), that makes a new data table from a matrix, and a data table method, GetAsMatrix(), that makes a matrix from the numeric columns in a table.

There is also a way to index data tables directly with data table subscripting.


// make a matrix of 5 rows, 3 cols, random values
mat = J( 5, 3, Random Normal() );
// make a data table from a matrix
dt = As Table( mat );
// make a matrix from a data table
mat2 = dt << GetAsMatrix;
// comparing two matrices returns a matrix of answers
// and the All function returns true if all of the
// matrix values are true
Show( All( mat == mat2 ) );
// use data table subscripting to get a matrix of all
// rows and all columns from the table; the zero subscript
// is used with matrix and data table to mean "all
// rows or all columns"
mat3 = dt[0, 0];
Show( All( mat == mat3 ) );

Discussion

<<GetAsMatrix can also specify the columns several ways, see the scripting index. AsTable can also name the columns, see the scripting index. The data table subscripting method has many more possibilities than this example suggests.


See Also

https://www.jmp.com/support/help/Matrices.shtml

https://community.jmp.com/t5/Uncharted/What-is-the-JSL-Matrix/ba-p/33349

Comments
Tedwang

Thanks, very useful , and any convient way to get a row vector from a data table?

Thanks!

Thanks!

 Data table subscripting describes indexing a data table as a matrix, and What is the JSL Matrix? shows how to use matrix subscripting. So, with a data table (or a matrix), rather than copying the data out of the table to make a matrix, you can get a row from either one like this:

// if dt is a table...
rowVector = dt[ rowNumber, 0 ]; // all columns
rowVector = dt[ rowNumber, { age, height, weight } ]; // selected columns
// if m is a matrix
rowVector = m[ row, 0 ];
rowVector = m[ row, 5::9 ]; // selected items from row
// and both dimensions at once
patch = m[ 3::5, 7::11 ];

The zero subscript means "all of this dimension". The matrix indexing starts at one.

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.