简体   繁体   English

基于x,y乘以z单元格值填充单元格

[英]Populate cells based on x by y by z cell value

I'm trying to populate cells based on n-1 values from three different cells. 我试图基于来自三个不同单元格的n-1值填充单元格。 I was successful in the x by y but I'm having trouble with z 我在x by y中取得了成功,但我遇到了z问题

For example, I have input: 例如,我输入了:

x     y     z
5     2     2

Output should be: 输出应该是:

x should have 0, 1, 2, 3, 4; x应该有0,1,2,3,4; each repeated twice 每次重复两次
y should have 0 and 1; y应该有0和1; each repeated five times 每次重复五次
z should have 0 and 1; z应该为0和1; each repeated five times but not the same output as x or y 每次重复五次,但输出与x或y不同

x   y   z
0   1   0   
1   0   0  
2   1   0  
3   0   0  
4   1   0  
0   0   0  
1   1   0  
2   0   0  
3   1   0  
4   0   0  
0   1   1  
1   0   1  
2   1   1  
3   0   1  
4   1   1  
0   0   1  
1   1   1  
2   0   1  
3   1   1  
4   0   1  

I used: 我用了:

for x 对于x
=IF(ROW()<=1+A$1*A$2*A$3,INT((ROW()-2)/(A$2*A$3)),"0") = IF(ROW()<= 1 + A $ 1 * A $ 2 * A $ 3 INT((ROW() - 2)/(A $ 2 * A $ 3)), “0”)

for y 因为你
=IF(ROW()<=1+A$1*A$2*A$3,MOD(ROW()-1,A$3),"0") = IF(ROW()<= 1 + A $ 1 * A $ 2 * A $ 3 MOD(ROW() - 1,A $ 3), “0”)

for z 为z
=IF(ROW()<=1+A$1*A$2*A$3,MOD(ROW()-1,A$2),"0") = IF(ROW()<= 1 + A $ 1 * A $ 2 * A $ 3 MOD(ROW() - 1,A $ 2), “0”)

A1 to A3 has the number of items for xyz. A1到A3具有xyz的项目数。 Are there any suggestions on how I can do this? 关于我如何做到这一点有什么建议吗?

Sorry for the late answer. 抱歉回复晚了。 If you let yourself use VBA (since you asked for it on your comment), let's say you have the next sheet: 如果你让自己使用VBA (因为你在评论中提出了这个问题),让我们说你有下一张表:

ActiveSheet

You could place the next Macro on the button GO : 您可以将下一个Macro放在按钮GO

Sub CartesianProduct()
    'Constants:
        Dim top_x As Integer
            top_x = ActiveSheet.Cells(2, 1).Value - 1
        Dim top_y As Integer
            top_y = ActiveSheet.Cells(2, 2).Value - 1
        Dim top_z As Integer
            top_z = ActiveSheet.Cells(2, 3).Value - 1
    'Coordinates
        Dim x As Integer
        Dim y As Integer
        Dim u As Integer
    'Counter (row):
        Dim c As Integer
            c = 2
    '----------
        For x = 0 To top_x
            For y = 0 To top_y
                For z = 0 To top_z
                    ActiveSheet.Cells(c, 5).FormulaR1C1 = x
                    ActiveSheet.Cells(c, 6).FormulaR1C1 = y
                    ActiveSheet.Cells(c, 7).FormulaR1C1 = z
                    c = c + 1
                Next z
            Next y
        Next x
End Sub

Then, by clicking on GO , you fill the table with the set of the cartesian product of your sets: 然后,通过单击GO ,您可以使用集合中的笛卡尔积集合填充表格:

ActiveSheet

If you want to avoid points with coordinates with the same value, just make the following ajust: 如果您想避免坐标具有相同值的点,请执行以下操作:

    For x = 0 To top_x
        For y = 0 To top_y
            For z = 0 To top_z
                If x <> y Or x <> z Or y <> z Then
                    ActiveSheet.Cells(c, 5).FormulaR1C1 = x
                    ActiveSheet.Cells(c, 6).FormulaR1C1 = y
                    ActiveSheet.Cells(c, 7).FormulaR1C1 = z
                    c = c + 1
                End If
            Next z
        Next y
    Next x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM