Home » excel » excel – Transpose row four columns at a time (row to 4-column matrix)

excel – Transpose row four columns at a time (row to 4-column matrix)

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have data in the following layout (each ‘digit’ in its own column but all in Row 1 of Excel):

1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7

How might I easily convert this to:

1 2 3 4  
5 6 7 8  
9 1 2 3  
4 5 6 7

I want every four pieces of data to be added, in separate columns, to new rows underneath one another.

How to&Answers:

If your data starts in A1, please try in A3, copied across to D and down to suit:

=OFFSET($A$1,,COLUMN()+4*(ROW()-2)-5)

Answer:

This is already answered, but just for fun, here’s a version that’s non-volatile and doesn’t need to be placed in a particular location:

=INDEX($A$1:$P$1,4*(ROW($A$1:$D$4)-1)+COLUMN($A$1:$D$4))

Replace the range $A$1:$P$1 above with whatever range you want to rearrange, and enter as an array formula. (Select the entire 4×4 range, type the formula, and press ctrl-shift-enter when you’re finished rather than just enter.) Note that $A$1:$D$4 is a constant, used to obtain the sequential numbers.

One more way – a shorter formula but requiring some setup:

Create a named range, let’s call it Matrix1:

1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1

You can put the matrix on another, possibly hidden, worksheet, or you can just use an array constant:

={1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1}

And another, let’s call it Matrix2:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

or:

={1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1}

Then the formula is just:

=MMULT(Matrix1*$A$2:$P$2,Matrix2)

Again, select the entire 4×4 range and enter as an array formula.

You could, of course, enter the array constants into the formula directly, but that gets unwieldy:

=MMULT({1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0;0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0;0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0;0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1}*$A$2:$P$2,{1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1;1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1})