Statistical programming, matrix languages, and more

Converting a Vector into a Matrix

Accepted Solution Solved
Reply
Contributor
Posts: 33
Accepted Solution

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: 3,408

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

View solution in original post


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

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 1462 views
  • 0 likes
  • 2 in conversation