Community Trekker

Joined:

Sep 15, 2014

## Is there an alternative to Shape function

All,

I would like to know if there is a good alternative to the "Shape" function (even UDF's - user defined functions  are welcome) .

``````// Generate Random Data
TestData = Random Shuffle(Random Index(10^4,10^3));

// Reshape the data using Shape function
TestData1 = Shape(TestData,10,N Rows(TestData)/10);

// Visualize data as data table
dt = As Table(TestData);
dt1 = As Table(TestData1);``````

Visualize original raw random data - dt1

Result of Shape function

What I am hoping to achieve is convert the long format in dt1 or screenshot1 into wide format i.e.

Take "n" elements of Column1 and form a new column .

This is acheived using reshape function in most environments like Matlab and R . Wondering if there is a better way to do this with "Shape" or an alternative function .

Best
Uday

Best
Uday
2 ACCEPTED SOLUTIONS

Accepted Solutions

Super User

Joined:

Jun 22, 2012

Solution

## Re: Is there an alternative to Shape function

You may get what you need from the pull down menus by using

Tables==>Split

or

Tables==>Transpose

Jim

Staff

Joined:

Jun 23, 2011

Solution

## Re: Is there an alternative to Shape function

It isn't wrong to mix matrix and data table operations but it isn't necessary, either. Matrices offer many advantages over data tables in a case like this, though. These simple examples illustrate how you might form matrices with random elements of any shape:

``````Names Default to Here( 1 );

n = 10;	// rows
m = 10; // columns

// generate matrix in steps
x = J( n*m, 1, Random Integer( 1, 6 ) );	// column vector of six-sided die rolls
x = Shape( x, n, m );						// form into columns row-wise
y = Transpose( x );							// other way
Show( x, y );								// see what you got

// generate a pattern matrix in a single step
i = 1;
x = J( n, m, i++ );							// row-wise
y = Transpose( x );							// other way
Show( x, y );								// see what you got

// generate a n by m matrix of random elements in a single step
x = J( n, m, Random Integer( 1, 6 ) );		// row-wise
y = Transpose( x );							// other way
Show( x, y );								// see what you got``````
Learn it once, use it forever!
6 REPLIES

Super User

Joined:

Jun 22, 2012

Solution

## Re: Is there an alternative to Shape function

You may get what you need from the pull down menus by using

Tables==>Split

or

Tables==>Transpose

Jim

Staff

Joined:

Jun 23, 2011

## Re: Is there an alternative to Shape function

In addition to Jim's answer, your script is really close. I would not expect JMP to work exactly the same as MATLAB or R, but it can accomplish the same results. The Shape() function in JMP works row-wise instead of column-wise as you expected. Simply apply the Transpose() function to the result returned by the Shape() function:

``````// Reshape the data using Shape function
TestData1 = Transpose( Shape(TestData,10,N Rows(TestData)/10) );``````

Learn it once, use it forever!

Community Trekker

Joined:

Sep 15, 2014

## Re: Is there an alternative to Shape function

@markbailey :
I have already tried that - however , the result looks like below - where I would have expected just 10 rows per column based on the function call .

@txnelson

Can you provide an example of how to exactly call "Split" function to accomplish this as well .

Best
Uday

Staff

Joined:

Jun 23, 2011

Solution

## Re: Is there an alternative to Shape function

It isn't wrong to mix matrix and data table operations but it isn't necessary, either. Matrices offer many advantages over data tables in a case like this, though. These simple examples illustrate how you might form matrices with random elements of any shape:

``````Names Default to Here( 1 );

n = 10;	// rows
m = 10; // columns

// generate matrix in steps
x = J( n*m, 1, Random Integer( 1, 6 ) );	// column vector of six-sided die rolls
x = Shape( x, n, m );						// form into columns row-wise
y = Transpose( x );							// other way
Show( x, y );								// see what you got

// generate a pattern matrix in a single step
i = 1;
x = J( n, m, i++ );							// row-wise
y = Transpose( x );							// other way
Show( x, y );								// see what you got

// generate a n by m matrix of random elements in a single step
x = J( n, m, Random Integer( 1, 6 ) );		// row-wise
y = Transpose( x );							// other way
Show( x, y );								// see what you got``````
Learn it once, use it forever!

Staff

Joined:

Jun 23, 2011

## Re: Is there an alternative to Shape function

When you become used to vectorized operations, you will think about the shape the whole way through, forwards and backwards. Applying only the Transpose() function won't give you what you want, of course. You have to change the Shape() function call in concert. In particular, you have to swap the number of rows and columns when you shape the original vector in anticipation of the transposition. Like this:

``````// Generate Random Data
TestData = Random Index( 10^4, 10^3 );

// Reshape the data using Shape function
TestData1 = Transpose( Shape( TestData, N Rows(TestData)/10, 10 ) );

// Visualize data as data table
dt = As Table( TestData );
dt1 = As Table( TestData1 );``````
Learn it once, use it forever!

Super User

Joined:

Jun 22, 2012

## Re: Is there an alternative to Shape function

Run the

File==>Split

it will produce a new data table.  Go to the "Table Panel" at the upper left side of the new table.  There you will find an entry named "Source".  Right click on it and select "Edit".  It will show you the code it used to create the new data table.

Jim