比较两个阵列-水平与垂直 [英] Comparing two arrays - Horizontal vs Vertical

查看:94
本文介绍了比较两个阵列-水平与垂直的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况如何

我正在尝试比较两个数组.为简单起见,让我们假设我们想知道一个数组的值在另一个数组中存在的频率.

我引用/查找的数组数据位于A1:A3

Apple
Lemon
Pear

我的搜索数组不在工作表中,而是写为{"Apple","Pear"}

问题

因此,要知道我们的搜索值在lookuparray中存在的频率,我们可以应用以下公式:

{=SUMPRODUCT(--(range1=range2))}

但是,{=SUMPRODUCT(--({"Apple","Pear"}=A1:A3))}会产生错误.换句话说,查找数组未按预期工作.

首先使用{=SUMPRODUCT(--({"Apple","Pear"}=TRANSPOSE(A1:A3)))}使用TRANSPOSE()函数从我的数据创建水平阵列的结果是正确的答案为2!

好像我的类型化数组被自动处理为水平数组,并且我的数据显然本来是垂直的.

为了检验我的假设,我尝试了另一个公式:

{=SUMPRODUCT(--({"Apple","Pear"}={"Apple","Lemon","Pear"}))}

两者都是类型化数组,因此使用上述逻辑,它们都将是水平数组,完全可以在不使用TRANSPOSE()的情况下工作,但是这将返回错误! #N/A

再次{=SUMPRODUCT(--({"Apple","Pear"}=TRANSPOSE({"Apple","Lemon","Pear"})))}给出了正确的答案2.

问题

有人可以向我解释吗?

  • 不能将水平数组与垂直数组进行比较的原因.
  • 为什么类型化数组会被自动处理为水平
  • 为什么在我的假设检验中,第二种类型的数组被视为垂直数组.

我真的很好奇,很高兴能链接到适当的文档,因为到目前为止我还没有找到任何文档.

这可能是一个容易回答的问题,尽管我似乎无法理解逻辑.

解决方案

有人可以向我解释吗?

  • 不能将水平数组与垂直数组进行比较的原因.

这实际上是可行的,您还可以将水平数组与其他水平数组进行比较.

出现错误的原因是由于数组长度不匹配.考虑以下数组:

执行=SUMPRODUCT(--(B3:D3=F3:G3))=SUMPRODUCT(--({"Apple","Lemon","Pear"}={"Apple","Pear"}))相同(在excel的英语版本中,我不确定100%确定其他版本的定界符),结果为=SUMPRODUCT(--(Apple=Apple, Lemon=Pear, Pear=???)),即第一个数组的第n个元素将其与第二个数组的第n个元素进行比较,如果没有匹配项-第一个数组中的第三个元素是Pear但第二个数组中没有第三个元素-那么您得到N/A.

当您比较两个数组(一个垂直和一个水平)时,excel实际上会扩展"最终数组.考虑以下内容(1行x 3col和2行x 1col):

执行=SUMPRODUCT(--(B3:D3=F3:F4))=SUMPRODUCT(--({"Apple","Lemon","Pear"}={"Apple";"Pear"}))相同,结果为=SUMPRODUCT(--(Apple=Apple, Lemon=Apple, Pear=Apple; Apple=Pear, Lemon=Pear, Pear=Pear)).基本上感觉就像Excel扩展了这样的两个数组(3col x 2row):

我认为,仅当一个数组的行高为1行而另一个数组的行宽为1列时,才会发生这种扩展",因此,如果采用的数组具有不同的值,则excel将返回尝试将元素与"nothing"进行比较以提供N/A(您可以使用公式"标签下的评估公式"功能提供帮助)

因此,本质上excel会得到一些类似于此的东西,其中第一个数组乘以第二个数组,得到结果数组:

但是由于最后一行和最后一列都包含空格,所以在那里您会得到N/A.

  • 为什么类型化数组会被自动处理为水平

在您的问题中,似乎,分隔行,因此使用=SUMPRODUCT(--({"Apple","Pear"}=A1:A3))时,您观察到的效果类似于我第一个示例中两行的比较,而使用=SUMPRODUCT(--({"Apple","Pear"}=TRANSPOSE(A1:A3)))时,您得到的是'扩展'发生.

如评论中所述,

在excel的英语版本中,,分隔列,而;分隔行,在这个简单的示例中可以看到,在该示例中,我提供了一个包含2行3列的数组,excel显示了{0,0,0;0,0,0}:

  • 为什么在我的假设检验中,第二种类型的数组被视为垂直数组.

TRANSPOSE只需将数组从垂直切换为水平(反之亦然),但是根据您要执行的操作,根据我的回答的第一部分,您将获得不同的结果,因此您要么当Excel无法将一个数组的项与另一个数组的另一个项匹配,或者两个数组的扩展"导致更大的数组时,具有N/A.

What is the case

I'm trying to compare two arrays. For simplicity sake let's assume we want to know how often the values of one array exist in the other array.

My referenced/lookup array data sits in A1:A3

Apple
Lemon
Pear

My search array is NOT in the worksheet, but written {"Apple","Pear"}

Problem

So to know how often our search values exists in the lookuparray we can apply a formula like:

{=SUMPRODUCT(--(range1=range2))}

However, {=SUMPRODUCT(--({"Apple","Pear"}=A1:A3))} produces an error. In other words the lookup array wasn't working as expected.

What did work was using TRANSPOSE() function to create a horizontal array from my data first using {=SUMPRODUCT(--({"Apple","Pear"}=TRANSPOSE(A1:A3)))} resulting in the correct answer of 2!

It seems as though my typed array is automatically handled as an horizontal array, and my data obviously was originally vertical.

To test my hypotheses I tried another formula:

{=SUMPRODUCT(--({"Apple","Pear"}={"Apple","Lemon","Pear"}))}

Both are typed arrays, so with above logic it would both be horizontal arrays, perfectly able to work without using TRANSPOSE(), however this returns an error! #N/A

Again {=SUMPRODUCT(--({"Apple","Pear"}=TRANSPOSE({"Apple","Lemon","Pear"})))} gave a correct answer of 2.

Question

Can someone please explain to me:

  • The reasoning why horizontal can't be compared to vertical arrays.
  • Why a typed array would automatically be handled as horizontal
  • Why in my test of the hypotheses the second typed array was handled as vertical.

I'm really curious, and would also be happy to be linked to appropriate documentation as so far I have not been able to find any.

This might be an easy one to answer, though I can't seem to get my head around the logic.

解决方案

Can someone please explain to me:

  • The reasoning why horizontal can't be compared to vertical arrays.

This is actually possible, and you can also compare horizontal arrays with other horizontal arrays.

The reason you have been getting the error is because of the mismatch in the length of the array. Consider the following arrays:

Doing =SUMPRODUCT(--(B3:D3=F3:G3)) is the same (on excel's english version, I'm not 100% sure on the delimiters on other versions) as =SUMPRODUCT(--({"Apple","Lemon","Pear"}={"Apple","Pear"})) and results in =SUMPRODUCT(--(Apple=Apple, Lemon=Pear, Pear=???)), that is the nth element of the first array is compared to the nth element of the second array, and if there is nothing to match --the 3rd element in the 1st array is Pear but there is no 3rd element for the 2nd array-- then you get N/A.

When you compare two arrays, one vertical and one horizontal, excel actually 'expands' the final array. Consider the following (1row x 3col and 2row x 1col):

Doing =SUMPRODUCT(--(B3:D3=F3:F4)) is the same as =SUMPRODUCT(--({"Apple","Lemon","Pear"}={"Apple";"Pear"})) and results in =SUMPRODUCT(--(Apple=Apple, Lemon=Apple, Pear=Apple; Apple=Pear, Lemon=Pear, Pear=Pear)). Basically it feels like Excel expanded the two arrays like this (3col x 2row):

This 'expansion' only happens when one array is 1 row high and the other is 1 column wide I believe, so if you take arrays that have something different, then excel will go back to trying to compare an element with 'nothing' to give N/A (you can use the Evaluate Formula feature under Formula tab to help):

So essentially excel is getting something a bit similar to this, where the first array is multiplied to the second array, giving the result array:

But since the last row and last column involve blanks, you get N/A there.

  • Why a typed array would automatically be handled as horizontal

In your question, it would seem that , delimit rows, so with =SUMPRODUCT(--({"Apple","Pear"}=A1:A3)) you are observing similar to the comparison of two rows in my first example, while with =SUMPRODUCT(--({"Apple","Pear"}=TRANSPOSE(A1:A3))), you are getting the 'expansion' occurring.

As stated in the comments, on the English version of excel, , delimits columns and ; delimits rows, as can be observed in this simple example where I supply an array with 2 rows and 3 columns, excel shows {0,0,0;0,0,0}:

  • Why in my test of the hypotheses the second typed array was handled as vertical.

TRANSPOSE simply switches an array from vertical to horizontal (and vice versa), but depending on what you are trying to do, you'll get different results as per the first part of my answer, so you'll either have N/A when excel cannot match an item of an array with another item of the other array, or 'expansion' of the two arrays that results in a bigger array.

这篇关于比较两个阵列-水平与垂直的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆