Comparing two arrays - Horizontal vs Vertical

2020-02-11 06:54发布

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

1条回答
贪生不怕死
2楼-- · 2020-02-11 07:32

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:

enter image description here

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):

enter image description here

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):

enter image description here

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):

enter image description here

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:

enter image description here

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}:

enter image description here

  • 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.

查看更多
登录 后发表回答