Solved
Contributor
Posts: 33

Converting a Vector into a Matrix

Hi everyone!

Maybe you could help me with my latest obstacle I've encountered with IML. Do you happen to know how to convert a vector into a matrix?

Here is a little example to give you a better idea:

Given:

Vector n (1000x1)

Goal:

Convert vector n into a 50x20 matrix m. The first column of matrix m should be equal to the first 50 elements of vector n. The second column should be equal to the next 50 elements and so on.

It would be great if you had a solution for this problem... Thanks a million!!!

Accepted Solutions
Solution
‎11-16-2011 04:36 PM
SAS Super FREQ
Posts: 4,181

Converting a Vector into a Matrix

Use the SHAPE function.

A blog article on reshaping matrices is here: http://blogs.sas.com/content/iml/2010/10/11/how-can-you-reshape-a-matrix/

The SHAPE function arranges the elements rowwise, so the first ROW of m contains the first consecutive elements:

v = 1:1000;

w = shape(v, 20, 50); /* w is 20 x 50 */

To get the data in column-wise order, transpose w:

m = T(w); /* m is 50 x 20 */

Rick

Statistical programming blog: http://blogs.sas.com/content/iml

All Replies
Solution
‎11-16-2011 04:36 PM
SAS Super FREQ
Posts: 4,181

Converting a Vector into a Matrix

Use the SHAPE function.

A blog article on reshaping matrices is here: http://blogs.sas.com/content/iml/2010/10/11/how-can-you-reshape-a-matrix/

The SHAPE function arranges the elements rowwise, so the first ROW of m contains the first consecutive elements:

v = 1:1000;

w = shape(v, 20, 50); /* w is 20 x 50 */

To get the data in column-wise order, transpose w:

m = T(w); /* m is 50 x 20 */

Rick

Statistical programming blog: http://blogs.sas.com/content/iml

Contributor
Posts: 33

Converting a Vector into a Matrix

Wow... that went fast! Thank you so much Rick!!!

🔒 This topic is solved and locked.