Convert an array of numbers into a 3x4 matrix

For questions that don't fall into one of the categories above, such as working with the RATS interface, using Wizards, etc.

Convert an array of numbers into a 3x4 matrix

Postby wljames » Wed Apr 18, 2012 2:04 pm

I am learning RATS. I want to convert an array, say 1,2,3, . . . ,12, into a 3x4 matrix. Thanks!
wljames
 
Posts: 3
Joined: Fri Dec 02, 2011 9:50 pm
Location: Durham, NC

Re: Convert an array of numbers into a 3x4 matrix

Postby TomDoan » Thu Apr 19, 2012 10:02 am

Use %RESHAPE. For instance

dec vect myvect(12)
ewise myvect(i)=i
compute my3by4=%reshape(myvect,3,4)

That will give

1 4 7 10
2 5 8 11
3 6 9 12

since RATS fills matrices by columns. If you want to reshape in the other direction, you would do

compute my3by4=tr(%reshape(myvect,4,3))

which will give

1 2 3 4
5 6 7 8
9 10 11 12
TomDoan
 
Posts: 2717
Joined: Wed Nov 01, 2006 5:36 pm


Return to Other RATS Usage Questions

Who is online

Users browsing this forum: No registered users and 1 guest