在给定条件下生成0和1的数组 [英] Generate an array of 0 and 1 with given conditions

查看:135
本文介绍了在给定条件下生成0和1的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据单元格中的值生成数组?例如,如果(A1,B1)为(3,0),(A2,B2)为(2,1),并且(A3,B3)为(2,0)(第一个数字表示第二个数字数组中的数字),如何获得{0,0,0,1,1,0,0}的数组?在一般情况下,如果我有(i,0),(j,1)和(k,0),我想得到一个{i零,j个,k零}的数组。

How does one generate an array according to value in cells? For example, if (A1,B1) is (3,0), (A2,B2) is (2,1), and (A3,B3) is (2,0) (this first number depicts how many of the second number in the array), how does one obtain an array of {0,0,0,1,1,0,0}? In a general form, if I have (i,0), (j,1), and (k,0), I would like to get an array of {i zeros,j ones, k zeros}.

几个Excel函数可以返回一个数组,例如OFFSET,INDEX。我不知道连接数组的任何方法。

Several Excel functions can return an array, such as OFFSET, INDEX. I don't know any way to concatenate arrays.

有什么方法吗?

推荐答案

如果使用评估公式逐步完成此操作,则应看到它会按要求生成一个数组:

If you step through this with Evaluate Formula, you should see that it generates an array as requested:

=SUM(INDEX(B:B,N(IF({1},MATCH(ROW(A1:INDEX(A:A,SUM(A:A))),IFERROR(SUBTOTAL(9,OFFSET(A1,0,0,ROW(A1:INDEX(A:A,COUNT(A:A)))-1,1))+1,1),1)))))

必须使用 Ctrl Shift Enter

基本上,它使用offset和小计来生成包含对A列中的值求和,然后使用match和index在此数组中查找1到7的数字,并在B列中找到相应的数字。不是很优雅,因为我必须使用零高度的偏移量来获取查找数组中的第一个值,然后是iferror,以设置结果#REF!到1。

Basically it uses offset and subtotal to generate an array containing a running total of the values in column A, then uses match and index to lookup numbers from 1 to 7 in this array and find the corresponding number in column B. Not very elegant because I had to use an offset with a height of zero to get the first value in the lookup array, then iferror to set the resulting #REF! to 1.

这是一个如何连接这样的数组的单独问题-如果您具有Excel 2019或365,则可以使用Textjoin。

It's a separate question of how to concatenate such an array - if you have Excel 2019 or 365 you can use Textjoin.

编辑

如果希望避免使用诸如Offset之类的易失函数,则可以获取A列的运行总计另一种方法是,通过如下开发二维数组并使用Mmult:

If you wish to avoid using volatile functions like Offset, you can get the running totals of column A another way, by developing a 2d array as follows and using Mmult:

0 0 0

1 0 0

1 1 0

公式:

=SUM(INDEX(B:B,N(IF({1},MATCH(ROW(A1:INDEX(A:A,SUM(A:A))),
MMULT(IF(ROW(A1:INDEX($1:$1048576,COUNT(A:A),COUNT(A:A)))>COLUMN(A1:INDEX($1:$1048576,COUNT(A:A),COUNT(A:A))),1,0),
A1:INDEX(A:A,COUNT(A:A)))+1)))))

这篇关于在给定条件下生成0和1的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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