I need to transpose a vector into a 2D matrix according to a group of values that are equal in another column of a matrix. For example:
1 x1
1 x2
1 x3
1 x4
2 x5
2 x6
2 x7
2 x8
Should look like:
x1 x2 x3 x4;
x5 x6 x7 x8;
This is the same procedure you would do in SAS using proc tabulate. Reshape didn’t work for me because it doesn’t transpose it, and tried permute with no luck either. Is there any built in command that does this besides having to program it in using find, transpose, and vertcat?
If for some reason you want to avoid
reshape, although the solution in comments will work, you can usesub2indto get the linear indices of the new matrix V, given that your first column will always provide the new line sub:The above, according to your specs, will work as long as there is an equal number of unique elements in
X, so you can form anMxNmatrix.