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
=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