Convert an array of numbers into a 3x4 matrix
Convert an array of numbers into a 3x4 matrix
I am learning RATS. I want to convert an array, say 1,2,3, . . . ,12, into a 3x4 matrix. Thanks!
Re: Convert an array of numbers into a 3x4 matrix
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
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