我有一个如下的矩阵, 我可以使用命令A_unique = unique(A,'rows')提取此矩阵中的A_unique = unique(A,'rows') ,结果如下 我需要找到每行存在于主矩阵A的次数 如何找到唯一行数? 有人可以帮忙吗? 提前致谢 马努 ...
提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
编码
Option Explicit
Sub countRearrange()
' Define constants.
Const srcName As String = "Sheet1"
Const srcAddress As String = "G2:J4"
Const dstName As String = "Sheet2"
Const dstAddress As String = "S2:Z4"
' Define workbook.
Dim wb As Workbook: Set wb = ThisWorkbook ' Workbook containing this code.
' Write values from Source Range to Data Array.
Dim Data As Variant: Data = wb.Worksheets(srcName).Range(srcAddress).Value
Dim cCount As Long: cCount = UBound(Data, 2)
' Define Destination Range.
Dim rg As Range: Set rg = wb.Worksheets(dstName).Range(dstAddress)
' Define Result Array.
Dim Result As Variant
ReDim Result(1 To rg.Rows.Count, 1 To rg.Columns.Count)
' Declare additional variables.
Dim Key As Variant ' Current Data Array Value
Dim i As Long ' Data Array Row Counter, Result Array Row Counter
Dim j As Long ' Data Array Column Counter
Dim n As Long ' Dictionary Element Counter, Result Array Column Counter
Dim x As Long ' Result Array Column Counter
' Write values from Data Array to Result Array.
With CreateObject("Scripting.Dictionary")
.CompareMode = vbTextCompare
For i = 1 To UBound(Data, 1)
For j = 1 To cCount
Key = Data(i, j)
If Not IsError(Key) Then
If Len(Key) > 0 Then
If Not .Exists(Key) Then
n = n + 1
.Item(Key) = n
Result(i, 2 * n - 1) = Key
Result(i, 2 * n) = 1
Else
x = .Item(Key)
Result(i, 2 * x - 1) = Key
Result(i, 2 * x) = Result(i, 2 * x) + 1
End If
End If
End If
Next j
Next i
End With
' Write values from Result Array to Destination Range.
rg.Value = Result
End Sub
您也可以使用 Excel 2010+ 中提供的Power Query
来执行此操作
Data => Get & Transform => From Table/Range
Home => Advanced Editor
并将 M 代码粘贴到打开的 window 中Applied Steps
window 以了解其工作原理M代码
let
Source = Excel.CurrentWorkbook(){[Name="Table11"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Item #1", type text}, {"Item #2", type text}, {"Item #3", type text}, {"Item #4", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Grouped Rows" = Table.Group(#"Added Index", {"Index"}, {
{"Grouped", each _, type table [#"Item #1"=nullable text, #"Item #2"=nullable text, #"Item #3"=nullable text, #"Item #4"=nullable text, Index=number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "uniqueItemList", each
let
//Get list of relevant column Names for grouping of each subTable
colNames = Table.ColumnNames(Source),
tbl1 = Table.SelectColumns([Grouped],colNames),
//Transpose the table
transp = Table.Transpose(tbl1),
//Get a count of each unique Item
grp = Table.Group(transp,"Column1",{"count", each List.Count([Column1])}),
//create Lists of the items and their count and "Zip" them together
col1 = Table.Column(grp,"Column1"),
count = Table.Column(grp,"count"),
zip = List.Zip({col1,count}),
//create the Label string
label = List.Accumulate(zip,"", (state, current)=>
if state = ""
then current{0} & ";" & Text.From(current{1})
else
if List.NonNullCount(current) = 2
then state & ";" & current{0} & ";" & Text.From(current{1})
else state)
in
label),
//split the label string into separate columns
#"Split Column by Delimiter" = Table.SplitColumn(#"Added Custom", "uniqueItemList", Splitter.SplitTextByDelimiter
(";", QuoteStyle.Csv), {"Slot #1", "Slot #1 Qty", "Slot #2", "Slot #2 Qty", "Slot #3", "Slot #3 Qty","Slot #4", "Slot #4 Qty"}),
//Delete unneeded columns
#"Removed Columns" = Table.RemoveColumns(#"Split Column by Delimiter",{"Index", "Grouped"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Slot #1", type text}, {"Slot #1 Qty", Int64.Type}, {"Slot #2", type text}, {"Slot #2 Qty", Int64.Type}, {"Slot #3", type text}, {"Slot #3 Qty", Int64.Type}, {"Slot #4", type text}, {"Slot #4 Qty", Int64.Type}})
in
#"Changed Type1"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.