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.
wljames
Posts: 3
Joined: Fri Dec 02, 2011 8:50 pm

Convert an array of numbers into a 3x4 matrix

Unread post by wljames »

I am learning RATS. I want to convert an array, say 1,2,3, . . . ,12, into a 3x4 matrix. Thanks!
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post by TomDoan »

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
Post Reply