<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Combinations of a list put into columns in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/598106#M80179</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The NChooseK Matrix( ) function is tailor-made for problems like this. It is also reasonably quick, and especially useful for k&amp;gt;2. Check out the code below for an example of 2 things, and 5 things, chosen from a set of 26.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);

lis = words("abcdefghijklmnopqrstuvwxyz","");

// original example: choose 2
dt = astable( nchoosekMatrix(nitems(lis), 2), &amp;lt;&amp;lt; column names({"x", "y"}) );
dt &amp;lt;&amp;lt; new column ("first", character, &amp;lt;&amp;lt; set values ( lis[dt:x &amp;lt;&amp;lt; get values]));
dt &amp;lt;&amp;lt; new column ("second", character, &amp;lt;&amp;lt; set values ( lis[dt:y &amp;lt;&amp;lt; get values]));
dt &amp;lt;&amp;lt; delete columns (1::2);

// a more interesting example: choose 5
dt = astable( nchoosekMatrix(nitems(lis), 5), &amp;lt;&amp;lt; column names( words("x1,x2,x3,x4,x5",",") ) );
for each ({ i }, 1::5, //i = 1;
	eval (substitute( expr(dt &amp;lt;&amp;lt; new column ( "y"||char(i), character, &amp;lt;&amp;lt;set values ( lis[_EX_ &amp;lt;&amp;lt; get values]  ) ) ), 
								expr(_EX_), 
								parse(evalinsert("dt:x^i^"))
	));
);
dt &amp;lt;&amp;lt; delete columns (1::5);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 07 Feb 2023 17:29:04 GMT</pubDate>
    <dc:creator>brady_brady</dc:creator>
    <dc:date>2023-02-07T17:29:04Z</dc:date>
    <item>
      <title>Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/555454#M76976</link>
      <description>&lt;P&gt;I'm trying to take a list of things and make all possible pairs of combinations appear in a set of two columns.&amp;nbsp; I currently have a script generated that does permutations (plus repeates of the same value in col a vs col b) but I'm struggling on how to reduce it down to combinations. For example, if the list of things was 1, 2, 3, 4, 5 then I don't need both 1/2 and 2/1, or 1/1.&amp;nbsp; Below is my script I currently have and I think I'm close, I just need a bit more to get where I need to be. I just need to make this script strictly do the choose 2 combinations of an arbitrary list. This particular example is using 5 things for simplicity.&amp;nbsp; thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; get values;
working = New Table( "TestAPIComb", New Column( "API10" ), New Column( "API102" ), add rows( N Items( APIs ) ^ 2 ) );
APIs2 = Test:API 10 &amp;lt;&amp;lt; get values;

theRow = 0;
	
For( APICNT = 1, APICNT &amp;lt;= N Items( APIs ), APICNT++,
	For( API2CNT = 1, API2CNT &amp;lt;= N Items( APIs2 ), API2CNT++,
		theRow++;
		:API10[theRow] = APIs[APICNT];
		:API102[theRow] = APIs[API2CNT];
	)
);	&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 09 Jun 2023 16:00:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/555454#M76976</guid>
      <dc:creator>JM13</dc:creator>
      <dc:date>2023-06-09T16:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/555646#M76981</link>
      <description>&lt;P&gt;I have added a few lines of code that will eliminate the duplicate combinations.&amp;nbsp; In addition, I would suggest that you look into&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;Tables=&amp;gt;Join&lt;/P&gt;
&lt;P&gt;at the Cartesian join capability.&amp;nbsp;&lt;/P&gt;
&lt;DIV id="tinyMceEditor_2879895f1bf2b9txnelson_0" class="mceNonEditable lia-copypaste-placeholder"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_1-1665586963727.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/46253iD0029079DC4A8B17/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_1-1665586963727.png" alt="txnelson_1-1665586963727.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It produces the all possible combinations much faster than JSL code can do it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;&amp;lt; get values;
working = New Table( "TestAPIComb", New Column( "API10" ), New Column( "API102" ), add rows( N Items( APIs ) ^ 2 ) );
APIs2 = Test:API 10 &amp;lt;&amp;lt; get values;

theRow = 0;
	
For( APICNT = 1, APICNT &amp;lt;= N Items( APIs ), APICNT++,
	For( API2CNT = 1, API2CNT &amp;lt;= 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 &amp;lt;= N Rows( dt ), i++,
	If( :API102[i] &amp;gt; :API10[i],
		hold = :API10[i];
		:API10[i] = :API102[i];
		:API102[i] = hold;
	)
);

// Find the duplicates
dt &amp;lt;&amp;lt; Select Duplicate Rows( :API10, :API102 );

// Delete the duplicates
dt &amp;lt;&amp;lt; delete rows;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Oct 2022 15:02:57 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/555646#M76981</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-10-12T15:02:57Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/556357#M77029</link>
      <description>&lt;P&gt;Most likely there are quite many ways of doing this (the cartesian join &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt; is good starting point). Here is one option using just matrix calculations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 &amp;lt;= 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)),
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1665759472083.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/46306i5423211E9D88EDEA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1665759472083.png" alt="jthi_0-1665759472083.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2022 15:13:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/556357#M77029</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-10-14T15:13:46Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/558654#M77200</link>
      <description>&lt;P&gt;Thanks. I looked at what&amp;nbsp;&lt;A href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687" target="_blank"&gt;@txnelson&lt;/A&gt;&amp;nbsp;suggested and also implemented the cartesian join and it returned 15 results instead of 10. I then just added a simple select condition at the end where if the first column = the second column then I also deleted them. Do either of you know which method is faster in JMP? i'm working with a large dataset and I'll try that matrix method as well but just didn't know if either of you know what should be more efficient.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 20:17:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/558654#M77200</guid>
      <dc:creator>JM13</dc:creator>
      <dc:date>2022-10-20T20:17:47Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/558660#M77204</link>
      <description>&lt;P&gt;I don't know which method would work fastest.&amp;nbsp; My suggestion is to run both of them and see which is fastest.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Oct 2022 20:36:58 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/558660#M77204</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-10-20T20:36:58Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/597835#M80140</link>
      <description>&lt;P&gt;Hi &lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;, any ideas if the initial list is string instead of number? Since matrix only works for number data type.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 08:10:24 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/597835#M80140</guid>
      <dc:creator>nthai</dc:creator>
      <dc:date>2023-02-07T08:10:24Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/597943#M80155</link>
      <description>&lt;P&gt;Without much changes you could just use initial list as numbers and then create conversion table to join character values based on those numeric values.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 12:49:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/597943#M80155</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2023-02-07T12:49:26Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/598106#M80179</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The NChooseK Matrix( ) function is tailor-made for problems like this. It is also reasonably quick, and especially useful for k&amp;gt;2. Check out the code below for an example of 2 things, and 5 things, chosen from a set of 26.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Brady&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);

lis = words("abcdefghijklmnopqrstuvwxyz","");

// original example: choose 2
dt = astable( nchoosekMatrix(nitems(lis), 2), &amp;lt;&amp;lt; column names({"x", "y"}) );
dt &amp;lt;&amp;lt; new column ("first", character, &amp;lt;&amp;lt; set values ( lis[dt:x &amp;lt;&amp;lt; get values]));
dt &amp;lt;&amp;lt; new column ("second", character, &amp;lt;&amp;lt; set values ( lis[dt:y &amp;lt;&amp;lt; get values]));
dt &amp;lt;&amp;lt; delete columns (1::2);

// a more interesting example: choose 5
dt = astable( nchoosekMatrix(nitems(lis), 5), &amp;lt;&amp;lt; column names( words("x1,x2,x3,x4,x5",",") ) );
for each ({ i }, 1::5, //i = 1;
	eval (substitute( expr(dt &amp;lt;&amp;lt; new column ( "y"||char(i), character, &amp;lt;&amp;lt;set values ( lis[_EX_ &amp;lt;&amp;lt; get values]  ) ) ), 
								expr(_EX_), 
								parse(evalinsert("dt:x^i^"))
	));
);
dt &amp;lt;&amp;lt; delete columns (1::5);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 07 Feb 2023 17:29:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/598106#M80179</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2023-02-07T17:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/598117#M80181</link>
      <description>&lt;P&gt;As to performance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code below generates the almost 2 million row table of pairs from a set of 2000 in a little under 6 seconds, which is a bit less than half as long as looping took. I don't know whether such performance is suitable for your purposes. Hopefully it is a feasible option for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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 ) ) &amp;lt;&amp;lt; get keys;

t1 = HP Time();

// looping solution
dt = new table( "xx", add rows (2e6), &amp;lt;&amp;lt; new column ("X", character), &amp;lt;&amp;lt;new column ("y", character));
n = nitems(lis);
r=0;
for  each ( { i }, 1::(n-1), 
	for each( { j }, ( i + 1):: n, 
		r++;
		dt[ r, 1] = lis[ i ]; dt[ r, 2 ] = lis [ j ]		
	)
);

t2 = HP Time();


// nChooseK matrix solution
dt = As Table( NChooseK Matrix( N Items( lis ), 2 ), &amp;lt;&amp;lt;column names( {"x", "y"} ) );
dt &amp;lt;&amp;lt; New Column( "first", character, &amp;lt;&amp;lt;set values( lis[dt:x &amp;lt;&amp;lt; get values] ) );
dt &amp;lt;&amp;lt; New Column( "second", character, &amp;lt;&amp;lt;set values( lis[dt:y &amp;lt;&amp;lt; get values] ) );
dt &amp;lt;&amp;lt; delete columns( 1 :: 2 );

t3 = HP Time();

Print( Eval Insert( "Create table of pairs from loop: ^(t2-t1)/1e6^ seconds." ) );
Print( Eval Insert( "Create table of pairs from nChooseK matrix: ^(t3-t2)/1e6^ seconds." ) );
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="brady_brady_2-1675793555617.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/49899iFB925AED919E916E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="brady_brady_2-1675793555617.png" alt="brady_brady_2-1675793555617.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 18:12:48 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/598117#M80181</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2023-02-07T18:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: Combinations of a list put into columns</title>
      <link>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/598128#M80183</link>
      <description>&lt;P&gt;I should also mention that the examples I used were with strings. If you are using numbers, you can just use straight matrix ops, which are pretty fast... the 2 million row table is generated in 0.3 seconds, and the 26choose5 table in 0.04 seconds.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;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." ) );

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="brady_brady_1-1675794226468.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/49903i6CEDD725D89FC002/image-size/medium?v=v2&amp;amp;px=400" role="button" title="brady_brady_1-1675794226468.png" alt="brady_brady_1-1675794226468.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Feb 2023 18:26:12 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Combinations-of-a-list-put-into-columns/m-p/598128#M80183</guid>
      <dc:creator>brady_brady</dc:creator>
      <dc:date>2023-02-07T18:26:12Z</dc:date>
    </item>
  </channel>
</rss>

