Oct 11 2021 08:40 PM
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.
Oct 12 2021 02:46 AM
Oct 12 2021 03:42 AM
SolutionThis 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})
)
Oct 12 2021 05:28 AM
@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
Oct 12 2021 07:42 AM - edited Oct 12 2021 11:40 AM
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!
Oct 12 2021 07:47 AM
Oct 12 2021 12:05 PM - edited Oct 12 2021 12:06 PM
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.
Oct 12 2021 12:28 PM - edited Oct 12 2021 12:29 PM
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.
Oct 12 2021 02:22 PM
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.
Oct 12 2021 07:02 PM
Oct 13 2021 03:38 AM
"... 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.
Oct 13 2021 09:40 AM
@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.
Oct 13 2021 02:48 PM
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", "--- ")
)
Oct 17 2021 04:34 PM
Jun 17 2024 07:18 AM - edited Jul 02 2024 12:38 AM
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 :)
Jun 17 2024 10:28 AM
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.
Jun 18 2024 02:35 AM
...like you who can understand That one was easy, for many other solutions you've dev. that's a different story
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?
Jun 18 2024 03:15 AM
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 .
Jun 18 2024 04:44 AM
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:
Jun 18 2024 07:16 AM
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.
Oct 12 2021 03:42 AM
SolutionThis 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})
)