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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

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