如何在没有给定初始值的情况下启动“减少公式”计算(Excel 365)?

huangapple go评论64阅读模式
英文:

How does "Reduce formula" start caclulation without a given initial value (excel 365)

问题

让我们看看微软是怎么说的:

https://support.microsoft.com/en-us/office/reduce-function-42e39910-b345-45f3-84b8-0642b568b7cb

它说:

> Reduce 函数通过对每个值应用 LAMBDA 并返回累加器中的总值,将数组减少为累积值。

我已经练习了一下使用 Reduce 公式,当未指定初始值时感到困惑。

我首先尝试了一个简单的例子

如何在没有给定初始值的情况下启动“减少公式”计算(Excel 365)?

这个例子告诉我,如果未指定初始值,那么第一个累加器的值为空白。

然后我尝试了另一个:

如何在没有给定初始值的情况下启动“减少公式”计算(Excel 365)?

这个公式计算了4次,对应于数组元素的数量(即100,101,102,103)。

所以我们得到了一个底部带有 4个堆栈 的 E3:G5 和初始值200

一切都能理解,直到我尝试删除初始值:

如何在没有给定初始值的情况下启动“减少公式”计算(Excel 365)?

我的问题是:

  1. 在例子2中,v(100,101,102,103)没有显示在结果数组中。
    例子3的结果数组如何在底部包括一个‘100’?

    LAMBDA 中的公式(即 VSTACK(E3:G5,a))甚至不包括‘v’。
    如果未指定初始值,累加器‘a’是否将数组的第一个元素作为其值?

  2. 结果数组只有3个堆叠的 E3:G5。为什么?
    我猜测只有当累加器‘a’不为空时,计算才开始做它该做的事情(vstack)。
    所以它‘消耗’了一个计算来将100作为初始值。我的猜测正确吗?

英文:

Let's see what microsoft says:

https://support.microsoft.com/en-us/office/reduce-function-42e39910-b345-45f3-84b8-0642b568b7cb

It says:

> Reduce function reduces an array to an accumulated value by applying a
> LAMBDA to each value and returning the total value in the accumulator.

I've done some practice using Reduce formula and feel confused when initial value is not specified.

I first try a simple example

如何在没有给定初始值的情况下启动“减少公式”计算(Excel 365)?

This example tells me that if initial value is not specified,then the first accumulator value is blank.

Then I tried another:

如何在没有给定初始值的情况下启动“减少公式”计算(Excel 365)?

This formula calculats 4 times which corresponds to the number of array elements (i.e 100,101,102,103).

So we get a 4 stacks of E3:G5 and initial value 200 at the bottom.

Everything is understandable until I tried to remove inital value:

如何在没有给定初始值的情况下启动“减少公式”计算(Excel 365)?

My questions are:

1.In example 2,v (100,101,102,103) is not shown in the result array.
How does the result array of example3 include a '100' at the bottom?

Formula in LAMBDA (ie. VSTACK(E3:G5,a)) doesn't even include 'v'.
Does accumulator 'a' take the 1st element from the array as its value if initial value is not specified?

2.Result array only have 3 stacks of E3:G5. Why?
My guess is that calculation only starts to do what it have to do (vstack) when accumulator 'a' is not null. So it 'consumes' one calculation in order to take 100 to be its initial value. Is my guess correct?

答案1

得分: 5

我将你的问题链接发布到了LinkedIn Excel Lambda讨论组。

Diarmuid Early在YouTube上的帐号是@DimEarly,他有以下观点:

> 如果没有初始值,REDUCE会将数组的第一个元素作为初始值,然后迭代数组的其余元素。如果这导致一个空数组(即只有一个初始值),它将返回那个值。
>
> 有趣的是,这个函数的文档不仅没有解释这一点,还提供了一个具有误导性的示例。文章中的示例1省略了“初始值”参数,但它之所以能正常工作,只是巧合,因为1 = 1^2(如果您尝试并放入任何其他值,它会给出错误的答案)。
>
> 文章链接

英文:

I posted a link to your question to the LinkedIn Excel Lambda discussions group.

Diarmuid Early of @DimEarly on YouTube has this to say:

> If there's no initial value, REDUCE takes the first element of the
> array as the initial value, then iterates over the remaining elements
> of the array. If this results in an empty array (i.e. there was only
> one value to start with), it just returns that one value.
>
> Interestingly, the function write-up not only doesn't explain this,
> put provides an actively misleading example. Example 1 in the article
> below omits the 'initial value' argument, but it only works by
> coincidence because 1 = 1^2 (if you try it and put in any other value,
> it gives the wrong answer).
>
> https://support.microsoft.com/en-us/office/reduce-function-42e39910-b345-45f3-84b8-0642b568b7cb?ns=excel&version=90&syslcid=1033&uilcid=1033&appver=zxl900&helpid=xlmain11.chm60702&ui=en-us&rs=en-us&ad=us

答案2

得分: 1

REDUCE 函数表现出一些微妙的行为。

考虑以下一对,它们的区别在于 REDUCE 函数中是否存在 initial_value 参数。

=REDUCE(,SEQUENCE(4,,100),LAMBDA(a,v,a+v))
=REDUCE(0,SEQUENCE(4,,100),LAMBDA(a,v,a+v))

两者都提供相同的结果 406,并且作为数组 SEQUENCE(4,,100) 中元素的简单累加器。

然而,现在考虑

=REDUCE(,SEQUENCE(4,,100),LAMBDA(a,v,a+1))
=REDUCE(0,SEQUENCE(4,,100),LAMBDA(a,v,a+1))

它们提供不同的结果:前者是 103,而后者是 4。在两种情况下,REDUCE 遍历数组 SEQUENCE(4,100) 的 4 个元素,但两种情况之间的区别在于对于第一个元素的迭代结果:当没有为 REDUCE 提供 initial_value 参数时,这是 100(数组的第一个元素),而当显式将其设置为零时,结果为 1。如果这对特定公式的目的是统计数组 SEQUENCE(4,,100) 中的项目数量,那么显然第二个结果是所需的。

一个假设是,第二对的差异是因为 LAMBDA 函数的第三个参数不再明确引用同一函数的第二个参数 v。然而,这个假设是错误的,因为在一对中:

=REDUCE(,SEQUENCE(4,,100),LAMBDA(a,v,a+(v/v)))
=REDUCE(0,SEQUENCE(4,,100),LAMBDA(a,v,a+(v/v)))

虽然有明确的引用,但仍然得到了与第二对相同的结果。

编辑

可以通过注意到,在第一对中,对应于序列的第一个元素的迭代计算结果在两种情况下是相同的来“平方”这些规则。没有 initial_value 时,它只是序列的第一个元素,即 100。有了初始值,它就是结果 a+v,其中 a0initial_valuev100 的第一个元素。

英文:

The REDUCE function exhibits some subtle behaviours.

Consider, the following pair whose difference is the absence/presence of an initial_value argument in the REDUCE function.

=REDUCE(,SEQUENCE(4,,100),LAMBDA(a,v,a+v)) and
=REDUCE(0,SEQUENCE(4,,100),LAMBDA(a,v,a+v))

both provide the same result of 406 and act as a simple accumulator of the elements in the array SEQUENCE(4,,100).

However, now consider

=REDUCE(,SEQUENCE(4,,100),LAMBDA(a,v,a+1)) and
=REDUCE(0,SEQUENCE(4,,100),LAMBDA(a,v,a+1))

These provide different results: the former is 103 whilst the latter is 4. In both cases REDUCE iterates over the 4 array elements of the array SEQUENCE(4,100) but the difference between the two cases lies in the iteration result of the first element: this is 100 (the first element of the array) when no initial-value argument is supplied to REDUCE and 1 when it is explicitly set to zero. If the purpose of this particular pair of formulae is to count the number of items within the array SEQUENCE(4,,100), then the second result is clearly the one that is desired.

One hypothesis is that the difference in the second pair arises because the third argument of the LAMBDA function no longer explicitly refers to the same function's second argument, v. However, this hypothesis is false since in the pair

=REDUCE(,SEQUENCE(4,,100),LAMBDA(a,v,a+(v/v))) and
=REDUCE(0,SEQUENCE(4,,100),LAMBDA(a,v,a+(v/v)))

there is such an explicit reference but the same results as the second pair are still obtained.

EDIT

The rules can be "squared" by noting that for the first pair, the result of the iterative calculation corresponding to the first element of the sequence is the same in both cases. Without an initial_value it is simply the first element of the sequence ie 100. With the initial value it is the result a+v where a is the initial_value of 0 and v is the first element of 100.

huangapple
  • 本文由 发表于 2023年7月13日 19:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定