+ Reply to Thread
Results 1 to 15 of 15

Convert two-dimensional array elements to cumulative sums

  1. #1
    Registered User
    Join Date
    02-24-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    11

    Question Convert two-dimensional array elements to cumulative sums

    I have a single cell array formula that evaluates to a 6x7 matrix of integers:

    {46,59,0,0,0,0;46,59,68,35,0,0;46,59,0,0,0,0;46,59,68,35,29,7;46,0,0,0,0,0;46,59,68,35,0,0;46,0,0,0,0,0}

    I'm looking for a way to convert the elements within each row to their running row totals:

    {46,105,105,105,105,105;46,105,173,208,208,208;46,105,105,105,105,105;46,105,173,208,237,244;46,46,46,46,46,46;46,105,173,208,208,208;46,46,46,46,46,46}
    (corrected per FlameRetired's feedback below, this row omitted in initial post by mistake)

    I've tried a number of approaches to manipulate the array formula to produce this result, so far I've been unsuccessful.

    ...The SUBTOTAL and AGGREGATE functions are just returning #VALUE! errors. Presumably these only work on multi-cell arrays or array constants?
    ...I've tried to isolate the row values preceeding each element using INDEX, but can't seem to get the recursion to work correctly to sum them.

    Any ideas? I'm stuck and not sure what approach to pursue next. Perhaps a VBA user-defined function that evaluates a two-dimensional array as input, loops through each element to produce a row subtotal at each position?

    Maybe there is a mathematical way to do this that I'm overlooking? Some combination of matrix multiplication, division, transposition that will produce the cumulative row sums?

    Thanks in advance for your help.
    Last edited by jakebryant; 07-05-2018 at 08:45 PM. Reason: corrected desired result array which was missing its 5th of 7 rows.

  2. #2
    Forum Expert tim201110's Avatar
    Join Date
    10-23-2011
    Location
    Russia
    MS-Off Ver
    2016, 2019
    Posts
    2,357

    Re: Convert two-dimensional array elements to cumulative sums

    for one row
    =SUM(TRANSPOSE(MMULT(--(ROW(1:6)>=COLUMN(A:F)),TRANSPOSE(A1:F1))))

  3. #3
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Convert two-dimensional array elements to cumulative sums

    Not sure I understand.

    From the description ... but not your results ... I interpret
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    which returns {105;208;105;244;46;208;46}
    at my end.

    What am I missing?
    Dave

  4. #4
    Registered User
    Join Date
    02-24-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Convert two-dimensional array elements to cumulative sums

    Quote Originally Posted by FlameRetired View Post
    Not sure I understand.

    From the description ... but not your results ... I interpret
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    which returns {105;208;105;244;46;208;46}
    at my end.

    What am I missing?
    This looks like the sum of all the elements in each row. Now the question is how to derive the "running total" of each row at each columnar position (e.g. {1,2,3} -> {1,3,6})? The resulting array should be the same dimensions (each element having the prior elements in the row added to it).

  5. #5
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Convert two-dimensional array elements to cumulative sums

    OK I'm starting to see a pattern.

    But, example shows you want to return a 6x6 array from a 6x7 array.

    Is that correct?

  6. #6
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Convert two-dimensional array elements to cumulative sums

    The resulting array should be the same dimensions (each element having the prior elements in the row added to it).
    In that case you expect another 6x7 array ... then would the desired result be

    {46,105,105,105,105,105;46,105,173,208,208,208;46,105,105,105,105,105;46,105,173,208,237,244;46,46,46,46,46,46;46,105,173,208,208,208;46,46,46,46,46,46}
    Last edited by FlameRetired; 07-05-2018 at 07:56 PM.

  7. #7
    Registered User
    Join Date
    02-24-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Convert two-dimensional array elements to cumulative sums

    Quote Originally Posted by FlameRetired View Post
    In that case you expect another 6x7 array ... then would the desired result be

    {46,105,105,105,105,105;46,105,173,208,208,208;46,105,105,105,105,105;46,105,173,208,237,244;46,46,46,46,46,46;46,105,173,208,208,208;46,46,46,46,46,46}
    Ah yes. You are correct. It looks like I ommitted the 5th result row by mistake in my initial post. Yours above is the desired result.

  8. #8
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Convert two-dimensional array elements to cumulative sums

    All I can come up with uses a lot of brute force. It is not robust. It requires 6x7 array.

    For brevity the array is named in Name Manager Arry.

    The formula
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Some how I doubt this acceptable.

  9. #9
    Registered User
    Join Date
    02-24-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Convert two-dimensional array elements to cumulative sums

    Quote Originally Posted by FlameRetired View Post
    All I can come up with uses a lot of brute force. It is not robust. It requires 6x7 array.

    For brevity the array is named in Name Manager Arry.

    The formula
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Some how I doubt this acceptable.
    It does give me something else to investigate in the choose() function, though. You're right, the trouble is that 6x7 array is dynamic, so the dimensions could be different. The problem is the recursion through the original array, which is what you've manually produced through brute force. I was playing around with that coerced INDEX(...,N(IF(1,...)) approach to isolate each element in each row, but it was inside the original array-producing function, so the recursion wasn't correct. Perhaps that choose() function might offer some more possibilities. Thanks for the lead, going to work on that for a bit and will post back for posterity if I come up with something.

  10. #10
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Convert two-dimensional array elements to cumulative sums

    I wish you the best.

    I suspect you are going to need a VBA approach to this. Unfortunately I don't know enough to help you there.

    I you want to go that route (Use Report Post) and ask a Mod/Admin to move your thread to the VBA section. Please don't do this yourself. That's double-posting. They'll yell at me for not telling you. LOL
    Last edited by FlameRetired; 07-05-2018 at 09:15 PM.

  11. #11
    Registered User
    Join Date
    02-24-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Convert two-dimensional array elements to cumulative sums

    Quote Originally Posted by FlameRetired View Post
    I wish you the best.

    I suspect you are going to need a VBA approach to this. Unfortunately I don't know enough to help you there.

    I you want to go that route (Use Report Post) and ask a Mod/Admin to move your thread to the VBA section. Please don't do this yourself. That's double-posting. They'll yell at me for not telling you. LOL
    Thanks, Dave. I'm going to do just that. With out VBA, I can't seem to find a way to manufacture a looping variable to increment. All the options for built-in functions (INDEX, SUMPRODUCT, CHOOSE, SUBTOTAL, AGGREGATE, etc.) can take arrays as inputs, but immediately evaluate those arrays, preventing recursion to modify the elements.

    I think the new approach will be a VBA user-defined function that takes an array as input, loops through each of the elements to calculate the row subtotal at each element's columnar position, and returns those running sums in an array of the same dimensions.

    Will begin working on this function, but I'm not very strong in VBA either, so any/all input from forum members is welcomed!

  12. #12
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP and 2007
    Posts
    15,829

    Re: Convert two-dimensional array elements to cumulative sums

    If any and all input is welcomed, I will offer this input -- is there some reason this must be done in a single cell array formula? I have long thought that the easiest way to get a "running total" like this is to have the array in a block of cells, then a simple SUM() function -- with the right mix of relative and absolute references -- easily computes the cumulative totals.

    If your array is in A3:F9, then your cumulative sum formula is =SUM($A3:A3) entered into H3 and copied across and down to N9. Without knowing the reasons for trying to force this calculation into a single cell, my first thought is how easy this is using helper ranges.
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  13. #13
    Registered User
    Join Date
    02-24-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Convert two-dimensional array elements to cumulative sums

    Quote Originally Posted by MrShorty View Post
    If any and all input is welcomed, I will offer this input -- is there some reason this must be done in a single cell array formula? I have long thought that the easiest way to get a "running total" like this is to have the array in a block of cells, then a simple SUM() function -- with the right mix of relative and absolute references -- easily computes the cumulative totals.

    If your array is in A3:F9, then your cumulative sum formula is =SUM($A3:A3) entered into H3 and copied across and down to N9. Without knowing the reasons for trying to force this calculation into a single cell, my first thought is how easy this is using helper ranges.
    You make a good point, but yes, there is some additional context I left out for simplicity.

    This cell is part of a worksheet that calculates pro-forma financial projections for a hospitality development. The array it produces describes the occupancy of all of the floorplans in the development in a given month (e.g. elements = # days occupied, columns = unique floorplans of accommodations, rows = # of guests in occupancy).

    So you see, the problem is that as the sheet moves left to right, each column is a new month's pro-forma projection, utilizing one of the sheet's two available dimensions. Converting this array from a single-cell formula to a multi-cell range would require adding a 3rd dimension to the sheet.

    I thought about using an altogether separate sheet in the workbook to host an 'occupancy grid' as an array formula spread across a 2D range, but it would only be able to calculate one month's configuration each time the workbook calculated. I would basically need a seperate 2D range for each month in the projection.

  14. #14
    Registered User
    Join Date
    02-24-2011
    Location
    Seattle
    MS-Off Ver
    Excel 2003
    Posts
    11

    Re: Convert two-dimensional array elements to cumulative sums

    Posting the stunningly simple VBA solution for posterity. This takes a two-dimensional array as input, and returns row-wise running sums at each columnar position.

    Please Login or Register  to view this content.

  15. #15
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 v 2403
    Posts
    13,406

    Re: Convert two-dimensional array elements to cumulative sums

    Bravo!

    Glad you found solution ... and a simple one at that. They often are.

    ... and thanks for posting for posterity. It often helps more than we know.

    If you would one more favor:

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. [SOLVED] Convert 2 dimensional array to 1 dimensional
    By jaryszek in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-19-2018, 05:20 AM
  2. Replies: 5
    Last Post: 12-03-2016, 07:18 AM
  3. [SOLVED] Convert one dimensional array into two dimensional array
    By mohammed sabr in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 04-26-2015, 10:34 AM
  4. [SOLVED] Convert Array set elements to Excel sheet and allow to user to add new data
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 10-16-2014, 06:11 PM
  5. [SOLVED] Convert two dimensional row to column data to a hard-coded array
    By evancharles in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-07-2013, 03:15 PM
  6. Filling all elements of a two dimensional array?
    By jerseyguy1996 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-03-2009, 11:38 AM
  7. How do I convert a row of cells into a two-dimensional array?
    By Glenn@stress in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 10-10-2005, 03:05 PM

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