將清單組合放入列中我試圖列出一個事物列表,並使所有可能的組合對出現在一組兩列中。我目前生成了一個執行排列的腳本(加上在 col a 和 col b 中重複相同的值),但我正在努力研究如何將其減少為組合。 例如,如果清單是 1、2、3、4、5,那麼我不需要同時使用 1/2 和 2/1,或 1/1。以下是我目前擁有的腳本,我認為我已經接近了,我只需要更多一點即可到達我需要的地方。 我只需要讓這個腳本嚴格選擇任意清單的 2 個組合。 為了簡單起見,這個特定範例使用了 5 個東西。謝謝
Names Default To Here( 1 );
New Table( "Test",
Add Rows( 5 ),
New Column( "API 10", Numeric, "Continuous", Format( "Best", 15 ), Set Values( [4238939261, 4238939263, 4238939262, 4238939328, 4238939329] ) ),
);
Test = Data Table( "Test" );
APIs = Test:API 10 << get values;
working = New Table( "TestAPIComb", New Column( "API10" ), New Column( "API102" ), add rows( N Items( APIs ) ^ 2 ) );
APIs2 = Test:API 10 << get values;
theRow = 0;
For( APICNT = 1, APICNT <= N Items( APIs ), APICNT++,
For( API2CNT = 1, API2CNT <= N Items( APIs2 ), API2CNT++,
theRow++;
:API10[theRow] = APIs[APICNT];
:API102[theRow] = APIs[API2CNT];
)
);
品質和工藝工程回覆:將清單組合放入列中我還應該提到,我使用的範例是字串。 如果您使用數字,則可以使用直接矩陣運算,這非常快...... 200 萬行表在 0.3 秒內生成,26choose5 表在 0.04 秒內生成。
Names Default To Here(1);
nums = J(2e3, 1, randominteger(1e9));
t1 = hptime();
astable(shape(nums[nChoosekMatrix( 2e3, 2 )], ., 2));
t2 = hptime();
Print( Eval Insert( "Create table of 2 numbers chosen from 2000: ^(t2-t1)/1e6^ seconds." ) );
// 26 choose 5
nums = J(26, 1, randominteger(1e9));
t3 = hptime();
astable(shape(nums[nChoosekMatrix( 26, 5 )], ., 5));
t4 = hptime();
Print( Eval Insert( "Create table of 5 numbers chosen from 26: ^(t4-t3)/1e6^ seconds." ) );

回覆:將清單組合放入列中至於性能。
下面的程式碼在不到 6 秒的時間內從 2000 個行的集合中產生了近 200 萬行的對錶,這略低於循環所用時間的一半。 不知道這樣的表現是否適合你的目的。 希望這對您來說是一個可行的選擇。
Names Default To Here( 1 );
letters = Words( "abcdefghijklmnopqrstuvwxyz", "" );
// create a large list of random strings of length 10. use an AA to remove (unlikely) duplicates
lis = Associative Array( Repeat( {Concat Items( letters[J( 10, 1, Random Integer( 26 ) )], "" )}, 2e3 ) ) << get keys;
t1 = HP Time();
// looping solution
dt = new table( "xx", add rows (2e6), << new column ("X", character), <<NEW column=""></NEW>

回覆:將清單組合放入列中你好,
NchooseK Matrix( ) 函數就是為此類問題量身定制的。 它也相當快,對於 k>2 特別有用。 請參閱下面的程式碼,以了解從 26 項中選擇的 2 項和 5 項的範例。
乾杯,
布雷迪
names default to here(1);
lis = words("abcdefghijklmnopqrstuvwxyz","");
// original example: choose 2
dt = astable( nchoosekMatrix(nitems(lis), 2), << column names({"x", "y"}) );
dt << new column ("first", character, << set values ( lis[dt:x << get values]));
dt << new column ("second", character, << set values ( lis[dt:y << get values]));
dt << delete columns (1::2);
// a more interesting example: choose 5
dt = astable( nchoosekMatrix(nitems(lis), 5), << column names( words("x1,x2,x3,x4,x5",",") ) );
for each ({ i }, 1::5, //i = 1;
eval (substitute( expr(dt << new column ( "y"||char(i), character, <<SET values=""></SET>回覆:將清單組合放入列中無需進行太多更改,您只需使用初始清單作為數字,然後建立轉換表以根據這些數值連接字元值。
回覆:將清單組合放入列中你好@jthi ,如果初始列表是字串而不是數字,有什麼想法嗎? 由於矩陣僅適用於數字資料類型。
回覆:將清單組合放入列中我不知道哪種方法效果最快。我的建議是運行它們,看看哪個最快。
回覆:將清單組合放入列中謝謝。 我看了看什麼@tx尼爾森建議並實現了笛卡爾連接,它返回 15 個結果而不是 10 個。 然後,我只是在末尾添加了一個簡單的選擇條件,如果第一列=第二列,那麼我也刪除了它們。 你們知道 JMP 中哪一種方法比較快嗎?我正在處理一個大型資料集,我也會嘗試該矩陣方法,但只是不知道你們中的任何一個人是否知道什麼應該更有效。
回覆:將清單組合放入列中最有可能的是有很多方法可以做到這一點(笛卡爾連接@tx尼爾森是一個很好的起點)。 這是僅使用矩陣計算的一種選項:
Names Default To Here(1);
// input list
m = [4238939261, 4238939263, 4238939262, 4238939328, 4238939329];
n_items = N Items(m);
// combination count
comb_count = Summation(i = 1, n_items - 1, i++);
m = Sort Ascending(m);
first_col = [];
second_col = [];
For(i = 2, i <= n_items, i++,
first_col ||= Repeat(m[i - 1], n_items - i + 1)`;
second_col ||= m[Index(i, n_items)]`;
);
dt = New Table("Combinations",
Add Rows(comb_count),
New Column("FirstCol", Numeric, Continuous, Values(first_col)),
New Column("SecondCol", Numeric, Continuous, Values(second_col)),
);

回覆:將清單組合放入列中我添加了幾行程式碼來消除重複的組合。另外,我建議你研究一下
表=>連接
在笛卡爾連接能力。

它產生所有可能的組合的速度比 JSL 程式碼快得多。
Names Default To Here( 1 );
New Table( "Test",
Add Rows( 5 ),
New Column( "API 10", Numeric, "Continuous", Format( "Best", 15 ), Set Values( [4238939261, 4238939263, 4238939262, 4238939328, 4238939329] ) ),
);
Test = Data Table( "Test" );
APIs = Test:API 10 << get values;
working = New Table( "TestAPIComb", New Column( "API10" ), New Column( "API102" ), add rows( N Items( APIs ) ^ 2 ) );
APIs2 = Test:API 10 << get values;
theRow = 0;
For( APICNT = 1, APICNT <= N Items( APIs ), APICNT++,
For( API2CNT = 1, API2CNT <= N Items( APIs2 ), API2CNT++,
theRow++;
:API10[theRow] = APIs[APICNT];
:API102[theRow] = APIs[API2CNT];
)
);
dt = Data Table( "TestAPIcomb" );
// Arange data so order is the same for all rows
For( i = 1, i <= N Rows( dt ), i++,
If( :API102[i] > :API10[i],
hold = :API10[i];
:API10[i] = :API102[i];
:API102[i] = hold;
)
);
// Find the duplicates
dt << Select Duplicate Rows( :API10, :API102 );
// Delete the duplicates
dt << delete rows;