+ Reply to Thread
Results 1 to 12 of 12

Reshaping a matrix with formulas

  1. #1
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    Germany
    MS-Off Ver
    365
    Posts
    490

    Reshaping a matrix with formulas

    Hi, I sort of understand how I transpose a matrix of the form n × m into m × n form
    and also how to reshape a matrix from the form n × m into p × q as long as n/q (respectively m*q) are integers.

    e.g. how to transform an 8 × 6 matrix into a 4 × 12 matrix ( 8 / 4 = 12 / 6 = 2).
    Whether the transformation is done row-wise or column-wise is not really important, so in my attached examples I went for row-wise, i.e. row by row.

    I now wish to do the same where the above condition is no longer satisifed, e.g. transforming a 5 × 4 matrix into a 2 x 10 matrix, i.e. 5 / 2 or 10 /4 = 2.5 (or more generalised, into any form, e.g. also a 7 × 3 matrix, acknowledging that the last data point cannot be populated, i.e. would be a zero/REF#! depending on whether this is done via OFFSET or INDEX).

    Attached examples.
    The first matrix is reshaped as explained above, so there're no problem there.
    Below that I try the more general approach, which fails starting at row 2: The first value to be picked up there should obviously be 70 and not (again) 6.

    Can somebody support me in changing the formulas accordingly?
    It seems that both the working of the row AND column determination need to be changed.
    Attached Files Attached Files

  2. #2
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,723

    Re: Reshaping a matrix with formulas

    I prefer to avoid OFFSET as it is volatile, so I'll try to work out a solution using INDEX with MOD and INT to generate the appropriate rows and columns.

    Pete

  3. #3
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    Germany
    MS-Off Ver
    365
    Posts
    490

    Re: Reshaping a matrix with formulas

    Quote Originally Posted by Pete_UK View Post
    I prefer to avoid OFFSET as it is volatile, so I'll try to work out a solution using INDEX with MOD and INT to generate the appropriate rows and columns.

    Pete
    Attached as INDEX version, maybe you prefer working with that one:
    At least the way the formulas are set up here, there's only 2 minimal adjustments needed to get to the same result via OFFSET or INDEX:
    - the starting point is a (fixed) range, rather than a cell
    - the row and columns shifts are increased each by 1 compared to the OFFSET alternative (basically because INDEX 1st value correspondenx to OFFSET shift by zero cells - hope you can follow me)

    Or a bit more illustratively:

    =INDEX($B$4:$G$11,INT((ROW()-ROW($B$21))*$G$19+((COLUMN()-COLUMN($B$21))/$D$15))+1,MOD((COLUMN()-COLUMN($B$21)+$H$15*(ROW()-ROW($B$21))),$D$15)+1)
    equals
    =OFFSET($B$4,INT((ROW()-ROW($B$21))*$G$19+((COLUMN()-COLUMN($B$21))/$D$15)),MOD((COLUMN()-COLUMN($B$21)+$H$15*(ROW()-ROW($B$21))),$D$15))

    I've managed so far to amend the formulas so these seem to work at the moment on both data sets, but it's a work in progress style, so I'm right in the middle trying to figure out how stable the formulas are.
    So no guarantees on nothing :D
    Attached Files Attached Files

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Reshaping a matrix with formulas

    How about
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  5. #5
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    Germany
    MS-Off Ver
    365
    Posts
    490

    Re: Reshaping a matrix with formulas

    Quote Originally Posted by Fluff13 View Post
    How about
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Never worked with filterxml; where do I start trying to understand this? xD

  6. #6
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Reshaping a matrix with formulas

    Please try

    =INDEX(FILTERXML("<x><m>"&TEXTJOIN("</m><m>",,B30:E34)&"</m></x>","//m"),SEQUENCE(H38,H36))

    or

    =LET(z,B30:E34,y,z+ROW(z)%+COLUMN(z)%%,INT(INDEX(MODE.MULT(y,y),SEQUENCE(H38,H36))))
    Attached Files Attached Files
    Last edited by Bo_Ry; 10-18-2021 at 02:05 PM.

  7. #7
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Reshaping a matrix with formulas

    Never worked with filterxml; where do I start trying to understand this?
    It basically just turns the text string into a vertical array

  8. #8
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    Germany
    MS-Off Ver
    365
    Posts
    490

    Re: Reshaping a matrix with formulas

    @Bo_Ry: On this version: =LET(z,B30:E34,y,z+ROW(z)%+COLUMN(z)%%,INT(INDEX(MODE.MULT(y,y),SEQUENCE(H38,H36))))
    What's the purpose of the % and also of the double %%? In the latter one, it doesn't seem to make a difference if one is deleted or if I switch rows and columns on the % and %%?

    @Bo_Ry / @Fluff: On your solutions, how would these formulas look instead if the matrices were to be reshaped columnwise?

    That is, first row would read: 19 / 100 / 6 / 24 / 90 / 18 / 23 etc.?

    Thx for your help so far. Much appreciated.

  9. #9
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Reshaping a matrix with formulas

    %% is added to make all number different so that MODE.MULT can pull out every number

    =MODE.MULT(B30:E34,B30:E34)
    with the duplicate number, it only shows 90 as 90 comes twice.

    If all number are unique then no need to add %% as in
    =MODE.MULT(M30:P34,M30:P34)

    columnwise

    =INDEX(FILTERXML("<x><m>"&TEXTJOIN("</m><m>",,TRANSPOSE(B30:E34))&"</m></x>","//m"),SEQUENCE(H38,H36))

    =LET(z,B30:E34,y,z+ROW(z)%+COLUMN(z)%%,INT(INDEX(MODE.MULT(TRANSPOSE(y),y),SEQUENCE(H38,H36))))

    =LET(s,SEQUENCE(H38,H36,0),INDEX(B30:E34,MOD(s,D38)+1,s/D38+1))
    Attached Files Attached Files

  10. #10
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,077

    Re: Reshaping a matrix with formulas

    For my suggestion, you can use transpose, like Bo_Ry did
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Although Bo_Ry's is much simpler than mine.

  11. #11
    Forum Guru Pete_UK's Avatar
    Join Date
    12-31-2011
    Location
    Warrington, England
    MS-Off Ver
    Office 2019 (still learning)
    Posts
    24,723

    Re: Reshaping a matrix with formulas

    Hi Raul,

    I don't have access to the functions FILTERXML, or SEQUENCE, or LET in my version of Excel, so I have had to approach this using fairly simple functions, and I have tried to explain my rationale in the attached file. In the transformed matrix we can use the ROWS and COLUMNS functions with a fixed first cell to generate a sequential series of numbers, although if we are trying to generate a 2 x 10 matrix from a 5 x 4 one then there are only 20 different combinations that can be generated from those functions. I've listed these in columns AD and AE. We need to transform these pairs of numbers into the pairs shown in AG and AH.

    In AJ6 I have used this formula:

    =(AD6-1)*10-(AD6-1)*1+AD6-1+AE6-1

    which when copied down produces a set of sequential numbers in the range from 0 to 19 (we can always add +1 on to get a range of 1 to 20). These formulae only use the numbers generated by ROWS($1:1) and COLUMNS($O:O). We now need to convert those sequential numbers into the patterns shown in AG and AH, by means of these two formulae:

    AL6: =INT(AJ6/4)+1

    AM6: =MOD(AJ6,4)+1

    Of course, AJ6 is an intermediate calculation, so we can substitute the expression from AJ6 into the cells stated below:

    AO6: =INT(((AD6-1)*10-(AD6-1)*1+AD6-1+AE6-1)/4)+1

    AP6: =MOD((AD6-1)*10-(AD6-1)*1+AD6-1+AE6-1,4)+1

    So now we are ready to build up an INDEX formula for the appropriate range and substituting the ROWS and COLUMNS terms for AD6 and AE6. I put this composite formula in cell O47:

    =INDEX($B$30:$E$34,INT(((ROWS($1:1)-1)*10-(ROWS($1:1)-1)*1+ROWS($1:1)-1+COLUMNS($O:O)-1)/4)+1,MOD(INT((ROWS($1:1)-1)/10)*10+COLUMNS($O:O)-1+(ROWS($1:1)-1)*10,4)+1)

    and copied across and down to suit the size of matrix we are trying to produce. Of course, this one is hard-coded for 10 columns and 4 rows, so I set up some named cells in bolded italic so I could take those values directly, and this is the formula in cell O42:

    =INDEX($B$30:$E$34,INT(((ROWS($1:1)-1)*new_cols2-(ROWS($1:1)-1)*1+ROWS($1:1)-1+COLUMNS($O:O)-1)/old_cols2)+1,MOD(INT((ROWS($1:1)-1)/new_cols2)*new_cols2+COLUMNS($O:O)-1+(ROWS($1:1)-1)*new_cols2,old_cols2)+1)

    Again, copied across and down to produce a 10 x 2 matrix. Another slight variation is shown in O21 for the upper matrix, i.e.:

    =IF(OR(COLUMNS($O:O)>new_cols,ROWS($1:1)>new_rows),"",INDEX($B$4:$G$11,INT(((ROWS($1:1)-1)*new_cols-(ROWS($1:1)-1)*1+ROWS($1:1)-1+COLUMNS($O:O)-1)/old_cols)+1,MOD(INT((ROWS($1:1)-1)/new_cols)*new_cols+COLUMNS($O:O)-1+(ROWS($1:1)-1)*new_cols,old_cols)+1))

    Here I've added an IF clause (in red) to cope with copying the formula too far in either direction (the yellow shading shows where I copied it to), as well as changed the INDEX range (purple) and the old and new cols and rows to suit this matrix.

    Notice that I've not needed to use the new_rows and old_rows named values, although I suspect I would need to if we were taking the data from down each column. I leave that as an exercise for the Reader (!!)

    Anyway, hope this helps, although you do already have two suggestions.

    Pete
    Attached Files Attached Files
    Last edited by Pete_UK; 10-19-2021 at 06:22 PM.

  12. #12
    Valued Forum Contributor
    Join Date
    02-06-2013
    Location
    Germany
    MS-Off Ver
    365
    Posts
    490

    Re: Reshaping a matrix with formulas

    Thanks a lot Pete, very instructive.
    That's a lot of effort you've put into doing this write-up!

    I will go through again later a bit more in detail.

    Just a comment from personal taste, pls don't take any offence: In terms of counters I personally prefer something like e.g. in A1: ROW()-ROW($A$1), which btw doesn't create a circular reference even though a cell is referring to itself, to something like this: ROWS($1:1).
    The reason is mainly file auditing: Referring to the subject cell itself will never create the issue that accidentally deleting another cell will compromise the formula by creating a REF#! error because that deleted (often blank) cell was used elsewhere for counting.
    I consider referring to entire columns or rows 'bad style' because effectively every cell in that column is referring to the formula cell: for each cells that's 6x over 16,000 cells and 3x over 1 million cells.
    Fortunately, such kind of references, as far I understand, don't result in slower calculation times. It's just counterintuitive why e.g. cell N1048572 is needed for a a bunch of calculations in rows 21 to 24.
    A further downside is that in Office 365 these types of formulas unnecessarily try to spill, so for me the formulas start like this: =@IF and this: =@INDEX, i.e. preventing the spilling behaviour.
    I try to avoid spilling where it's not needed.

    Anyway, cheers.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Reshaping several excel sheets into one panel data base
    By SaltySenator in forum Excel General
    Replies: 1
    Last Post: 04-17-2020, 04:20 AM
  2. [SOLVED] sum of matrix formulas for two criteria
    By trisoldee in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-09-2019, 04:19 PM
  3. [SOLVED] Reshaping date format
    By daregu91 in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 02-27-2018, 09:23 PM
  4. Problem with Reshaping Excel data for Stata use.
    By alex54 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-07-2014, 10:56 AM
  5. [SOLVED] Using Formulas matrix in VBA and drag down
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-06-2013, 08:29 PM
  6. Reshaping Data
    By GQuinn in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-21-2010, 11:41 PM
  7. Reshaping a matrix/text array
    By jesiotr in forum Excel General
    Replies: 1
    Last Post: 01-31-2008, 08:19 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1