BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MarkGIP
Calcite | Level 5

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

2 REPLIES 2
Rick_SAS
SAS Super FREQ

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

MarkGIP
Calcite | Level 5

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

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

Register now

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 4239 views
  • 0 likes
  • 2 in conversation