Group Abstract Group Abstract

Message Boards Message Boards

Generating a special m x n matrix?

Posted 9 years ago
POSTED BY: Ulrich Utiger
5 Replies

The number of elements 'm' can be given by:

Pochhammer[n, r]/r!

Check:

Table[Length[GiveList[n, r]], {n, 1, 7}, {r, 1, 7}] // Grid
Table[Pochhammer[n, r]/r!, {n, 1, 7}, {r, 1, 7}] // Grid
POSTED BY: Sander Huisman
Posted 9 years ago

@Sander: Wow, this works. Thanks a lot. I was trying something with an integer c incrementing it and transforming it into base r with IntegerDigits. Far too complicate...

@Bill: You have almost done it too. Thank you anyway. Maybe I can complete it and test it for speed against the method of Sander as I will use the function in a loop for big n and r.

POSTED BY: Ulrich Utiger
POSTED BY: Sander Huisman
Posted 9 years ago

.

f[n_, r_] := Select[IntegerDigits[Range[0, 2^n-1], r, n]+1, LessEqual @@ # &]

In[2]:= f[2, 2]

Out[2]= {{1, 1}, {1, 2}, {2, 2}}

In[3]:= f[3, 2]

Out[3]= {{1, 1, 1}, {1, 1, 2}, {1, 2, 2}, {2, 2, 2}}

I am not certain that I have interpreted your 'r' correctly.

POSTED BY: Bill Simpson

I'm not sure if I understand your description exactly but I think you can generalize your pattern like so:

ClearAll[GiveList]
(*GiveList[n_,r_]:=Select[Tuples[Range[r],n],And@@NonNegative[Differences[#]]&]*)
(*GiveList[n_, r_] := DeleteDuplicatesBy[Tuples[Range[r], n], Sort]*)
GiveList[n_Integer,r_Integer]:=Module[{vars,seq},
  vars=Table[Unique[],r];
  seq=MapThread[{#1,#2,n}&,{vars,Prepend[Most[vars],1]}];
  Flatten[Table[vars,Evaluate[Sequence@@seq]],r-1]
]
GiveList[2, 2]
GiveList[2, 3]
GiveList[3, 2]
GiveList[3, 3]

{{1,1},{1,2},{2,2}}
{{1,1},{1,2},{1,3},{2,2},{2,3},{3,3}}
{{1,1,1},{1,1,2},{1,2,2},{2,2,2}}
{{1,1,1},{1,1,2},{1,1,3},{1,2,2},{1,2,3},{1,3,3},{2,2,2},{2,2,3},{2,3,3},{3,3,3}}

I came up with multiple ways of implementing it, but the last one seems to be the nicest (and fastest) implementation I think...

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard