SOLVED

Product of numbers in a row in a dynamic array

Brass Contributor

How does one do row-by-row evaluations with a 2d dynamic array?

 

For example, I need the product of all the numbers in each row in an array. I can only seem to find ways to get the product of all the numbers in all rows of the array, reduced to a single number.

 

My spilled array:

 

0 2 2

2 1 1

3 3 2

 

Result sought:

 

0

2

18

 

from

 

0*2*2

2*1*1

3*3*2

 

I've struggled mightily and I'm about to resort to some very ugly text manipulation plus a name with evaluate based on the fact that I know my numbers will all be single digits :( I really do not want to do this!

 

=LET(
 rows, ROW(array)-ROW(INDEX(array,1,1)),
 width, COLUMNS(array),
 astext, TEXTJOIN("*",,array)&"*",
 MID(astext,rows*width*2+1,width*2-1)

)

 

It seems absurd that I can make four rows of text, but not fours rows of results.

 

Thanks for any insights.

 

 

 

21 Replies
best response confirmed by boukasa (Brass Contributor)
Solution

@boukasa 

This is something I have been complaining about for a couple of years or so now but, as @Jan Karel Pieterse says, the definitive solution is to be found in the Insiders beta Channel.  BYROW will return each row as a distinct range reference and supports any relevant calculation.

 

As an interim workaround, the multiplication of positive numbers may be performed by using logarithms

= EXP(
    MMULT(LN(array#), {1;1;1})
  )

 

@boukasa A Secong solution could be to get each dynamic columns using Index, and multiply them:

 

=INDEX(H4#;SEQUENCE(ROWS(H4#));1)*INDEX(H4#;SEQUENCE(ROWS(H4#));2)*INDEX(H4#;SEQUENCE(ROWS(H4#));3)

 

/Geir

Thanks so much for this workaround! I don't have access to the BYROW function yet, but your solution definitely works for my use case for now. My column count is dynamic so I've made your formula:

=EXP(MMULT(LN(array#),TRANSPOSE(SEQUENCE(1,COLUMNS(array#),1,0))))

Much appreciated!

Geir, thanks so much - my problem includes a dynamic number of columns. I don't fully grasp yet how Excel "views" an array, but I was surprised that you don't seem to be able to use an array for the row/column parameters of INDEX.

@boukasa 

I am not sure what you have in mind for this reply to @Geir Hogstad.   INDEX will accept array and lift the appropriate scalar parameters,  For example

 

= INDEX(array#, {2;5;3}, {4,2,5})

 

would assemble a 3x3 array from a larger array.  It would still have the same problem as the original,

i.e. functions like PRODUCT would still aggregate the entire array, giving a single value.  For row products, you would still need 

 

= BYROW( 
    INDEX(array#, {2;5;3}, {4,2,5}),
    LAMBDA(row, PRODUCT(row))
  )

 

I am just puzzled, not critical.

Peter, I really appreciate you taking the time to look at this. I may have expressed my observation incorrectly or inartfully. Here's an example of what I mean:

In A1, put =SEQUENCE(5,5)
In A7, put =ROW(A1#)-ROW(A1)+1
In B7, put =INDEX(A1#,A7#,0)
In H7, put =INDEX(A1#,A7,0)

I don't understand why B7 doesn't produce full rows (array as parameter to INDEX) but H7 does (non-array as parameter to INDEX). I expected B7 to produce a row, and I also expected to be able to write PRODUCT(INDEX(A1#,A7#,0)) to get my row product - but that not only doesn't work, it reduces to a single result for the whole A1# range.

@boukasa 

What you get from Microsoft is that Excel never has handled 'arrays of arrays' or 'arrays of ranges' and they need to maintain backward compatibility.  Now, however, it has become important because, with dynamic arrays, one keeps hitting the problem.  Your formula

=INDEX(A1#,A7,0)

returns a range object; by that, COLUMN would return sheet column numbers and ISREF would return TRUE.  As soon as you add the # to A7, Excel recognises that you want an array of ranges and blocks the calculation, instead returning an array of single values (it is not a range).

As for

=PRODUCT(INDEX(A1#,A7#,0))

that just suggests that Excel knew perfectly well what you wanted all along, but refused to give it to you!

 

Even with the helper functions MAKEARRAY etc., it can be difficult to return arrays of arrays.

Ha yes Excel reminds me of other relationships I have :)

I wanted to ask for your thoughts on one issue with the workaround formula you gave me. It doesn't work if there is a 0 in the array because ln(0) is an error. Is there a way to get that to work? I need the 0 result of the product as it has a semantic meaning.

@boukasa 

"... other relationships ..." big grin! 

 

The solution depends upon the semantic meaning.  Taking it as zero, rather than omitted:

= LET(
  selectedValues, FILTER(values,Tabell1[Option]=selected),
  ones, SEQUENCE(COLUMNS(selectedValues),,,0),
  logs, LN(ABS(selectedValues)),
  zeroTest, ISERROR(logs),
  arr, IF(zeroTest, 0, logs),
  result, MMULT(arr, ones),
  isZero, MMULT(SIGN(zeroTest), ones),
  IF(isZero, 0, EXP(result)))

Once you have Lambda functions

= LET(
  PRODUCTλ, LAMBDA(row, PRODUCT(row)),
  BYROW(array#, PRODUCTλ))

would be preferable.  An interesting features of these solutions is that neither looks remotely similar to a traditional spreadsheet formula.

@Peter Bartholomew I can't quite match/figure out how to match your suggestion to my structure. (This is a lot of hoops to get through given that ByRow is coming! But I don't have access to it.)

 

Here's what's really happening. I have a list of donors who will give to any of a list of nonprofits if certain criteria match between the donor's rules and criteria about the nonprofit. Each donor specifies criteria the she excludes or allows. For example, "allow North Texas, exclude Oklahoma, allow women's causes, exclude animal causes, allow this nonprofit, exclude that nonprofit."

 

I can pick a particular nonprofit, look up all its criteria, match it to the allow/exclude rules for the donors, and a complicated formula creates an array that looks like this:

 

                   criteria1     criteria2    criteria3

donor1       exclude     no rule       allow

donor2       allow         no rule       no rule

donor3       no rule      no rule       no rule

 

One or more excludes should exclude the donor, otherwise one or more allows should match the donor. No excludes and no allows also means the donor is not matched. Multiplication is a one-to-one map onto this logic if 0 is exclude, 1 is no rule, and >1 is allow.

 

              criteria1     criteria2    criteria3

donor1       0                1              2

donor2       2                1              1

donor3       1                1              1

 

By multiplying the numbers together, the result is any number >=2 means the donor can support this nonprofit. Any single exclude will result in 0. Any non-rule leaves the result alone (1). Any allow (2) will get the result out of the 0/1 range.

 

Since my result set is an array but not a table, I'm not sure how to use your filter suggestion. I mean "exclude any 0 in the row."

 

Eventually this will all end up in a database where it belongs, but boy Excel makes a proof of concept extremely fast and easy.

 

By the way, your comment about these lambdas being unlike anything seen in spreadsheets before, I had a thought about that. The array functions make the spreadsheet data much more like a database. Maybe one day there will be an integration of SQL directly into the spreadsheet. But I think it's interesting that these two things intersect: (a) lambda - row by row calculation - is exactly what you attempt to avoid in a query, and conversely (b) the "instant total 2d reduction" that I'm trying to get around here is actually quite difficult to make happen in SQL. So this whole powerful array/lambda paradigm seems to be at the intersection of the gap between (older) spreadsheets and SQL. 

@boukasa 

That changes everything.  You are not really interested in the product at all; it is merely an AND condition looking for an instance of "exclude".  Negate the condition and it can be combined by addition, representing OR.  

I have left the table because it resizes to accommodate fresh data, but no longer use the structured references.

= LET(
    selectedValues, FILTER(values,Tabell1[Option]=selected),
    ones,     SEQUENCE(COLUMNS(selectedValues),,,0),
    inc,      SIGN(selectedValues="allow"),
    exc,      SIGN(selectedValues="exclude"),
    included, MMULT(inc, ones),
    excluded, MMULT(exc, ones),
    IF(NOT(excluded) * included, "Match", "---   ")
  )

or with Lambda functions

= LET(
    INCLUDEλ, LAMBDA(row, OR(row="allow")),
    EXCLUDEλ, LAMBDA(row, OR(row="exclude")),
    excluded, BYROW(array#, EXCLUDEλ),
    included, BYROW(array#, INCLUDEλ),
    IF(NOT(excluded) * included, "Match", "---   ")
  )

 

Peter, yes this is the answer, and semantic. You have been incredibly generous with your time and knowledge. Thanks so much.

@Peter Bartholomew 

A friend of mine THANKs YOU++ for your option to perform a PRODUCT row by row w/2021. She also needed that it works with negative nums & to ignore BLANKs. We spent a couple of hours together... Attached is what we came up with (also ignore Text values & raises 1st error if any). Seems to work...
(we could observe tiny differences with PRODUCT on some sets but she can live with that :)

@Lorenzo 

A walk down memory lane.  How things have changed over the past 5 years!

The math is easily followed but you include a masterclass in error handling.  Maybe I should try to work it into @Craig Hatmaker's Component-Based Software Engineering modules.  Once they are nested several deep, it becomes difficult to identify the module and error type that is the source of a failed formula. 

An alternative way of counting  negatives could be

= IF(XOR(SIGN(nums < 0))

My formulas are so far from the beaten track of normal spreadsheeting that it is good to find someone like you who can understand and improve on my formulas.:smile:

 

@Peter Bartholomew 

...like you who can understand That one was easy, for many other solutions you've dev. that's a different story :facepalm:

 

And TBH I'm not clear re. your alternative to count negatives. I see the idea and understand after putting things on the grid. However, I don't see how this could return an array of TRUEs/FALSEs. Did I miss something again?

 

PS: I wrote a few articles for 2021 based on my friend needs/requests. Although I don't anticipate a bunch of people would need a PRODUCT by row/col. it would probably make sense to add it to the collection, just in case... May I say the initial solution is yours?

@Lorenzo 

I am sorry about messing up the code snippet; it was getting late and I messed up! A characteristic of XOR is that it alternates with the count of TRUE/FALSE as the list of arguments gets longer (somewhat strange behaviour).  So I think either of

= IF(XOR(nums < 0), -1, 1)

= IF(XOR(nums > 0), -1, 1)

will work.

 

Please feel free to use anything of interest that you pick up from my work as you choose.  Credit is always nice but not essential :smile:.

@Peter Bartholomew 

No worries at all. I did understand how to interpret your XOR suggestion from yesterday. However this doesn't appear to work and this really doesn't matter:

Sample.png

 

@Lorenzo 

You are quite right of course.  I had placed the formula snippet within BYROW; in which case you could have used

= BYROW(nums, PRODUCT)

in the first place!  My apologies.

1 best response

Accepted Solutions
best response confirmed by boukasa (Brass Contributor)
Solution

@boukasa 

This is something I have been complaining about for a couple of years or so now but, as @Jan Karel Pieterse says, the definitive solution is to be found in the Insiders beta Channel.  BYROW will return each row as a distinct range reference and supports any relevant calculation.

 

As an interim workaround, the multiplication of positive numbers may be performed by using logarithms

= EXP(
    MMULT(LN(array#), {1;1;1})
  )

 

View solution in original post