Excel 转置 - 锯齿状数组
Excel Transpose - Jagged Array
我需要将锯齿状数组转置或转换为 excel 中的多列,但遇到了一些困难。我的数据如下:
Round A1 A2 A3 A4 A5
1 1 2 3
2 4 5
3 6 7
4 8 9
所以我需要在遍历每一行的地方将其展平,并将每组序列号转换为单独的序列号,每行仅标记一列
SeqNum A1 A2 A3 A4 A5
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
我编辑了示例中的数字以使其更清晰。基本上,我想将每行有多个条目的第一个 table 展平到每行只有一个条目的第二个 table。
我想在 Excel
中实现以下算法
int numOfTotalElements = 0;
List<int> flattened = new List<int>();
int numRows = matrix.length;
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length; j++){
flattened.add(matrix[i][j])
}//end cols
}// end rows
foreach( int i in flattened){
System.Console.WriteLine(i);
}
看看其中之一是否可以让您到达您想去的地方。
Sub expandFlatten()
Dim c As Long, i As Long, j As Long
Dim vals As Variant, nuvals As Variant
With Worksheets("sheet2")
With .Cells(1, 1).CurrentRegion
vals = .Cells.Value2
End With
c = UBound(vals, 2) + 2
.Cells(1, c).CurrentRegion.Clear
.Cells(1, c).Resize(UBound(vals, 1), UBound(vals, 2)) = vals
With .Cells(1, c).CurrentRegion
.Cells(1, 1) = "SeqNum"
.Offset(1, 1).ClearContents
.Cells(2, 1).Resize(Application.Max(.Parent.Cells(1, 1).CurrentRegion), 1) _
.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
End With
With .Cells(1, c).CurrentRegion
ReDim nuvals(1 To .Rows.Count - 1, 1 To .Columns.Count - 1)
For i = LBound(vals, 1) + 1 To UBound(vals, 1)
For j = LBound(vals, 2) + 1 To UBound(vals, 2)
If Not IsEmpty(vals(i, j)) And IsNumeric(vals(i, j)) Then
nuvals(vals(i, j), j - 1) = vals(i, j)
End If
Next j
Next i
.Offset(1, 1).Resize(UBound(nuvals, 1), UBound(nuvals, 2)) = nuvals
End With
.Activate
.Cells(1, 1).Select
End With
End Sub
重新创建列 header 标签和右侧下方的数字序列(最多 MAX(B:F)
)后,将此标准公式放入 P2,
=IFERROR(INDEX(B:B, MATCH($O2,B:B, 0)), "")
向右和向下填充。
原始数据 ExpandFlatten() index(... , 匹配(...
我需要将锯齿状数组转置或转换为 excel 中的多列,但遇到了一些困难。我的数据如下:
Round A1 A2 A3 A4 A5
1 1 2 3
2 4 5
3 6 7
4 8 9
所以我需要在遍历每一行的地方将其展平,并将每组序列号转换为单独的序列号,每行仅标记一列
SeqNum A1 A2 A3 A4 A5
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
我编辑了示例中的数字以使其更清晰。基本上,我想将每行有多个条目的第一个 table 展平到每行只有一个条目的第二个 table。
我想在 Excel
中实现以下算法int numOfTotalElements = 0;
List<int> flattened = new List<int>();
int numRows = matrix.length;
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix[i].length; j++){
flattened.add(matrix[i][j])
}//end cols
}// end rows
foreach( int i in flattened){
System.Console.WriteLine(i);
}
看看其中之一是否可以让您到达您想去的地方。
Sub expandFlatten()
Dim c As Long, i As Long, j As Long
Dim vals As Variant, nuvals As Variant
With Worksheets("sheet2")
With .Cells(1, 1).CurrentRegion
vals = .Cells.Value2
End With
c = UBound(vals, 2) + 2
.Cells(1, c).CurrentRegion.Clear
.Cells(1, c).Resize(UBound(vals, 1), UBound(vals, 2)) = vals
With .Cells(1, c).CurrentRegion
.Cells(1, 1) = "SeqNum"
.Offset(1, 1).ClearContents
.Cells(2, 1).Resize(Application.Max(.Parent.Cells(1, 1).CurrentRegion), 1) _
.DataSeries Rowcol:=xlColumns, Type:=xlLinear, Step:=1
End With
With .Cells(1, c).CurrentRegion
ReDim nuvals(1 To .Rows.Count - 1, 1 To .Columns.Count - 1)
For i = LBound(vals, 1) + 1 To UBound(vals, 1)
For j = LBound(vals, 2) + 1 To UBound(vals, 2)
If Not IsEmpty(vals(i, j)) And IsNumeric(vals(i, j)) Then
nuvals(vals(i, j), j - 1) = vals(i, j)
End If
Next j
Next i
.Offset(1, 1).Resize(UBound(nuvals, 1), UBound(nuvals, 2)) = nuvals
End With
.Activate
.Cells(1, 1).Select
End With
End Sub
重新创建列 header 标签和右侧下方的数字序列(最多 MAX(B:F)
)后,将此标准公式放入 P2,
=IFERROR(INDEX(B:B, MATCH($O2,B:B, 0)), "")
向右和向下填充。
原始数据 ExpandFlatten() index(... , 匹配(...