I'm trying to populate cells based on n-1 values from three different cells. I was successful in the x by y but I'm having trouble with z
For example, I have input:
x y z
5 2 2
Output should be:
x should have 0, 1, 2, 3, 4; each repeated twice
y should have 0 and 1; each repeated five times
z should have 0 and 1; each repeated five times but not the same output as x or 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
=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")
for z
=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. 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:
You could place the next Macro
on the button 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:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.