How to create a n-dimensional array of all combination of number?
I wish to create a function AllCombnations(d, maxValue) which will create a d-dimensions array of all number combinations from 0 to maxValue.
For example, a hardcoded version of creating all number combinations in 3D space, from 0 to maxValue would possibly be something like:
for (int i = 0; i < maxValue; i++)
for (int j = 0; j < maxValue; j++)
for (int k = 0; k < maxValue; k++)
{
// code here
}
The issue I face is that I cannot nest n for loops, and am unsure how I would go about this. I have considered recursion, but have had no success. Any help would be greatly appreciated.
c# arrays
|
show 5 more comments
I wish to create a function AllCombnations(d, maxValue) which will create a d-dimensions array of all number combinations from 0 to maxValue.
For example, a hardcoded version of creating all number combinations in 3D space, from 0 to maxValue would possibly be something like:
for (int i = 0; i < maxValue; i++)
for (int j = 0; j < maxValue; j++)
for (int k = 0; k < maxValue; k++)
{
// code here
}
The issue I face is that I cannot nest n for loops, and am unsure how I would go about this. I have considered recursion, but have had no success. Any help would be greatly appreciated.
c# arrays
1
Please provide a small example of what you want the data to look like.
– 500 - Internal Server Error
Nov 22 '18 at 13:06
For example, something like{{0, 0, 0}, {0, 0, 1}, ..., {0, 0, maxValue}, ..., {maxValue, maxValue, maxValue}}
– TimeTravelPenguin
Nov 22 '18 at 13:10
what do you mean by number combinations
– preciousbetine
Nov 22 '18 at 13:12
I am working with a square/rectangular space in n-dimensions. All combinations is simply all the points within that defined space. Right now I am focusing on square spaces, so for 3D, the x, y, z coordinates all have the same region of 0->maxValue. I should note these positions are integer values. In other words, from(0,0,0)to say(10,10,10)and any number combination in between.
– TimeTravelPenguin
Nov 22 '18 at 13:15
What would be the point of such a table? With it, you could (in this case) look uparray[i, j, k]for anyi, j, k, but that would just give you what you used to look it up.
– 500 - Internal Server Error
Nov 22 '18 at 13:15
|
show 5 more comments
I wish to create a function AllCombnations(d, maxValue) which will create a d-dimensions array of all number combinations from 0 to maxValue.
For example, a hardcoded version of creating all number combinations in 3D space, from 0 to maxValue would possibly be something like:
for (int i = 0; i < maxValue; i++)
for (int j = 0; j < maxValue; j++)
for (int k = 0; k < maxValue; k++)
{
// code here
}
The issue I face is that I cannot nest n for loops, and am unsure how I would go about this. I have considered recursion, but have had no success. Any help would be greatly appreciated.
c# arrays
I wish to create a function AllCombnations(d, maxValue) which will create a d-dimensions array of all number combinations from 0 to maxValue.
For example, a hardcoded version of creating all number combinations in 3D space, from 0 to maxValue would possibly be something like:
for (int i = 0; i < maxValue; i++)
for (int j = 0; j < maxValue; j++)
for (int k = 0; k < maxValue; k++)
{
// code here
}
The issue I face is that I cannot nest n for loops, and am unsure how I would go about this. I have considered recursion, but have had no success. Any help would be greatly appreciated.
c# arrays
c# arrays
edited Nov 23 '18 at 7:39
Dmitry Bychenko
107k1093133
107k1093133
asked Nov 22 '18 at 13:03
TimeTravelPenguinTimeTravelPenguin
287
287
1
Please provide a small example of what you want the data to look like.
– 500 - Internal Server Error
Nov 22 '18 at 13:06
For example, something like{{0, 0, 0}, {0, 0, 1}, ..., {0, 0, maxValue}, ..., {maxValue, maxValue, maxValue}}
– TimeTravelPenguin
Nov 22 '18 at 13:10
what do you mean by number combinations
– preciousbetine
Nov 22 '18 at 13:12
I am working with a square/rectangular space in n-dimensions. All combinations is simply all the points within that defined space. Right now I am focusing on square spaces, so for 3D, the x, y, z coordinates all have the same region of 0->maxValue. I should note these positions are integer values. In other words, from(0,0,0)to say(10,10,10)and any number combination in between.
– TimeTravelPenguin
Nov 22 '18 at 13:15
What would be the point of such a table? With it, you could (in this case) look uparray[i, j, k]for anyi, j, k, but that would just give you what you used to look it up.
– 500 - Internal Server Error
Nov 22 '18 at 13:15
|
show 5 more comments
1
Please provide a small example of what you want the data to look like.
– 500 - Internal Server Error
Nov 22 '18 at 13:06
For example, something like{{0, 0, 0}, {0, 0, 1}, ..., {0, 0, maxValue}, ..., {maxValue, maxValue, maxValue}}
– TimeTravelPenguin
Nov 22 '18 at 13:10
what do you mean by number combinations
– preciousbetine
Nov 22 '18 at 13:12
I am working with a square/rectangular space in n-dimensions. All combinations is simply all the points within that defined space. Right now I am focusing on square spaces, so for 3D, the x, y, z coordinates all have the same region of 0->maxValue. I should note these positions are integer values. In other words, from(0,0,0)to say(10,10,10)and any number combination in between.
– TimeTravelPenguin
Nov 22 '18 at 13:15
What would be the point of such a table? With it, you could (in this case) look uparray[i, j, k]for anyi, j, k, but that would just give you what you used to look it up.
– 500 - Internal Server Error
Nov 22 '18 at 13:15
1
1
Please provide a small example of what you want the data to look like.
– 500 - Internal Server Error
Nov 22 '18 at 13:06
Please provide a small example of what you want the data to look like.
– 500 - Internal Server Error
Nov 22 '18 at 13:06
For example, something like
{{0, 0, 0}, {0, 0, 1}, ..., {0, 0, maxValue}, ..., {maxValue, maxValue, maxValue}}– TimeTravelPenguin
Nov 22 '18 at 13:10
For example, something like
{{0, 0, 0}, {0, 0, 1}, ..., {0, 0, maxValue}, ..., {maxValue, maxValue, maxValue}}– TimeTravelPenguin
Nov 22 '18 at 13:10
what do you mean by number combinations
– preciousbetine
Nov 22 '18 at 13:12
what do you mean by number combinations
– preciousbetine
Nov 22 '18 at 13:12
I am working with a square/rectangular space in n-dimensions. All combinations is simply all the points within that defined space. Right now I am focusing on square spaces, so for 3D, the x, y, z coordinates all have the same region of 0->
maxValue. I should note these positions are integer values. In other words, from (0,0,0) to say (10,10,10) and any number combination in between.– TimeTravelPenguin
Nov 22 '18 at 13:15
I am working with a square/rectangular space in n-dimensions. All combinations is simply all the points within that defined space. Right now I am focusing on square spaces, so for 3D, the x, y, z coordinates all have the same region of 0->
maxValue. I should note these positions are integer values. In other words, from (0,0,0) to say (10,10,10) and any number combination in between.– TimeTravelPenguin
Nov 22 '18 at 13:15
What would be the point of such a table? With it, you could (in this case) look up
array[i, j, k] for any i, j, k, but that would just give you what you used to look it up.– 500 - Internal Server Error
Nov 22 '18 at 13:15
What would be the point of such a table? With it, you could (in this case) look up
array[i, j, k] for any i, j, k, but that would just give you what you used to look it up.– 500 - Internal Server Error
Nov 22 '18 at 13:15
|
show 5 more comments
1 Answer
1
active
oldest
votes
Actually, you can loop over dimensions. Please, have a look at Array class
Demo:
// [6, 6, 6] array
int rank = 3; // 3D array - 3 dimensions
int maxValue = 6; // Each dimension is of size 6
int lengths = Enumerable // {6, 6, 6} - lengths of the dimensions:
.Repeat(maxValue, rank) // rank times maxValue
.ToArray(); // materialized as array
//TODO: put the right type of arrays' items
// In demo, let array be of type string: "string[6, 6, 6] array"
var array = Array.CreateInstance(typeof(string), lengths);
// we can't use hardcoded set (i, j, k) of variables
// we have to address array's item via array of rank length
int address = new int[array.Rank];
// Single loop over all array's items (and dimensions)
while (true) {
//TODO: put the right value here by given address:
// (i == address[0], j == address[1], k == address[2] etc.)
array.SetValue(
string.Concat(address.Select(i => (char) (i + 'A'))), // value: "AAA", "AAB" etc.
address); // address: [0,0,0], [0,0,1],
// here we compute next address
for (int i = 0; i < address.Length; ++i)
if (address[i] >= array.GetLength(i) - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
// if we get {0, 0, ..., 0} address, we've exhausted all the items
if (address.All(index => index == 0))
break;
}
Let's have a look at the array (20 top items):
Console.WriteLine(string.Join(Environment.NewLine, array.OfType<string>().Take(20)));
Outcome:
AAA
AAB
AAC
AAD
AAE
AAF
ABA
ABB
ABC
ABD
ABE
ABF
ACA
ACB
ACC
ACD
ACE
ACF
ADA
ADB
1
Thanks! I've not seen this before! I have only just picked up C# over another language, so this is advanced compared to my previous learnings, so I will look into this further. Thank you for the information!
– TimeTravelPenguin
Nov 22 '18 at 13:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431650%2fhow-to-create-a-n-dimensional-array-of-all-combination-of-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Actually, you can loop over dimensions. Please, have a look at Array class
Demo:
// [6, 6, 6] array
int rank = 3; // 3D array - 3 dimensions
int maxValue = 6; // Each dimension is of size 6
int lengths = Enumerable // {6, 6, 6} - lengths of the dimensions:
.Repeat(maxValue, rank) // rank times maxValue
.ToArray(); // materialized as array
//TODO: put the right type of arrays' items
// In demo, let array be of type string: "string[6, 6, 6] array"
var array = Array.CreateInstance(typeof(string), lengths);
// we can't use hardcoded set (i, j, k) of variables
// we have to address array's item via array of rank length
int address = new int[array.Rank];
// Single loop over all array's items (and dimensions)
while (true) {
//TODO: put the right value here by given address:
// (i == address[0], j == address[1], k == address[2] etc.)
array.SetValue(
string.Concat(address.Select(i => (char) (i + 'A'))), // value: "AAA", "AAB" etc.
address); // address: [0,0,0], [0,0,1],
// here we compute next address
for (int i = 0; i < address.Length; ++i)
if (address[i] >= array.GetLength(i) - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
// if we get {0, 0, ..., 0} address, we've exhausted all the items
if (address.All(index => index == 0))
break;
}
Let's have a look at the array (20 top items):
Console.WriteLine(string.Join(Environment.NewLine, array.OfType<string>().Take(20)));
Outcome:
AAA
AAB
AAC
AAD
AAE
AAF
ABA
ABB
ABC
ABD
ABE
ABF
ACA
ACB
ACC
ACD
ACE
ACF
ADA
ADB
1
Thanks! I've not seen this before! I have only just picked up C# over another language, so this is advanced compared to my previous learnings, so I will look into this further. Thank you for the information!
– TimeTravelPenguin
Nov 22 '18 at 13:35
add a comment |
Actually, you can loop over dimensions. Please, have a look at Array class
Demo:
// [6, 6, 6] array
int rank = 3; // 3D array - 3 dimensions
int maxValue = 6; // Each dimension is of size 6
int lengths = Enumerable // {6, 6, 6} - lengths of the dimensions:
.Repeat(maxValue, rank) // rank times maxValue
.ToArray(); // materialized as array
//TODO: put the right type of arrays' items
// In demo, let array be of type string: "string[6, 6, 6] array"
var array = Array.CreateInstance(typeof(string), lengths);
// we can't use hardcoded set (i, j, k) of variables
// we have to address array's item via array of rank length
int address = new int[array.Rank];
// Single loop over all array's items (and dimensions)
while (true) {
//TODO: put the right value here by given address:
// (i == address[0], j == address[1], k == address[2] etc.)
array.SetValue(
string.Concat(address.Select(i => (char) (i + 'A'))), // value: "AAA", "AAB" etc.
address); // address: [0,0,0], [0,0,1],
// here we compute next address
for (int i = 0; i < address.Length; ++i)
if (address[i] >= array.GetLength(i) - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
// if we get {0, 0, ..., 0} address, we've exhausted all the items
if (address.All(index => index == 0))
break;
}
Let's have a look at the array (20 top items):
Console.WriteLine(string.Join(Environment.NewLine, array.OfType<string>().Take(20)));
Outcome:
AAA
AAB
AAC
AAD
AAE
AAF
ABA
ABB
ABC
ABD
ABE
ABF
ACA
ACB
ACC
ACD
ACE
ACF
ADA
ADB
1
Thanks! I've not seen this before! I have only just picked up C# over another language, so this is advanced compared to my previous learnings, so I will look into this further. Thank you for the information!
– TimeTravelPenguin
Nov 22 '18 at 13:35
add a comment |
Actually, you can loop over dimensions. Please, have a look at Array class
Demo:
// [6, 6, 6] array
int rank = 3; // 3D array - 3 dimensions
int maxValue = 6; // Each dimension is of size 6
int lengths = Enumerable // {6, 6, 6} - lengths of the dimensions:
.Repeat(maxValue, rank) // rank times maxValue
.ToArray(); // materialized as array
//TODO: put the right type of arrays' items
// In demo, let array be of type string: "string[6, 6, 6] array"
var array = Array.CreateInstance(typeof(string), lengths);
// we can't use hardcoded set (i, j, k) of variables
// we have to address array's item via array of rank length
int address = new int[array.Rank];
// Single loop over all array's items (and dimensions)
while (true) {
//TODO: put the right value here by given address:
// (i == address[0], j == address[1], k == address[2] etc.)
array.SetValue(
string.Concat(address.Select(i => (char) (i + 'A'))), // value: "AAA", "AAB" etc.
address); // address: [0,0,0], [0,0,1],
// here we compute next address
for (int i = 0; i < address.Length; ++i)
if (address[i] >= array.GetLength(i) - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
// if we get {0, 0, ..., 0} address, we've exhausted all the items
if (address.All(index => index == 0))
break;
}
Let's have a look at the array (20 top items):
Console.WriteLine(string.Join(Environment.NewLine, array.OfType<string>().Take(20)));
Outcome:
AAA
AAB
AAC
AAD
AAE
AAF
ABA
ABB
ABC
ABD
ABE
ABF
ACA
ACB
ACC
ACD
ACE
ACF
ADA
ADB
Actually, you can loop over dimensions. Please, have a look at Array class
Demo:
// [6, 6, 6] array
int rank = 3; // 3D array - 3 dimensions
int maxValue = 6; // Each dimension is of size 6
int lengths = Enumerable // {6, 6, 6} - lengths of the dimensions:
.Repeat(maxValue, rank) // rank times maxValue
.ToArray(); // materialized as array
//TODO: put the right type of arrays' items
// In demo, let array be of type string: "string[6, 6, 6] array"
var array = Array.CreateInstance(typeof(string), lengths);
// we can't use hardcoded set (i, j, k) of variables
// we have to address array's item via array of rank length
int address = new int[array.Rank];
// Single loop over all array's items (and dimensions)
while (true) {
//TODO: put the right value here by given address:
// (i == address[0], j == address[1], k == address[2] etc.)
array.SetValue(
string.Concat(address.Select(i => (char) (i + 'A'))), // value: "AAA", "AAB" etc.
address); // address: [0,0,0], [0,0,1],
// here we compute next address
for (int i = 0; i < address.Length; ++i)
if (address[i] >= array.GetLength(i) - 1)
address[i] = 0;
else {
address[i] += 1;
break;
}
// if we get {0, 0, ..., 0} address, we've exhausted all the items
if (address.All(index => index == 0))
break;
}
Let's have a look at the array (20 top items):
Console.WriteLine(string.Join(Environment.NewLine, array.OfType<string>().Take(20)));
Outcome:
AAA
AAB
AAC
AAD
AAE
AAF
ABA
ABB
ABC
ABD
ABE
ABF
ACA
ACB
ACC
ACD
ACE
ACF
ADA
ADB
edited Nov 23 '18 at 7:24
answered Nov 22 '18 at 13:25
Dmitry BychenkoDmitry Bychenko
107k1093133
107k1093133
1
Thanks! I've not seen this before! I have only just picked up C# over another language, so this is advanced compared to my previous learnings, so I will look into this further. Thank you for the information!
– TimeTravelPenguin
Nov 22 '18 at 13:35
add a comment |
1
Thanks! I've not seen this before! I have only just picked up C# over another language, so this is advanced compared to my previous learnings, so I will look into this further. Thank you for the information!
– TimeTravelPenguin
Nov 22 '18 at 13:35
1
1
Thanks! I've not seen this before! I have only just picked up C# over another language, so this is advanced compared to my previous learnings, so I will look into this further. Thank you for the information!
– TimeTravelPenguin
Nov 22 '18 at 13:35
Thanks! I've not seen this before! I have only just picked up C# over another language, so this is advanced compared to my previous learnings, so I will look into this further. Thank you for the information!
– TimeTravelPenguin
Nov 22 '18 at 13:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431650%2fhow-to-create-a-n-dimensional-array-of-all-combination-of-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Please provide a small example of what you want the data to look like.
– 500 - Internal Server Error
Nov 22 '18 at 13:06
For example, something like
{{0, 0, 0}, {0, 0, 1}, ..., {0, 0, maxValue}, ..., {maxValue, maxValue, maxValue}}– TimeTravelPenguin
Nov 22 '18 at 13:10
what do you mean by number combinations
– preciousbetine
Nov 22 '18 at 13:12
I am working with a square/rectangular space in n-dimensions. All combinations is simply all the points within that defined space. Right now I am focusing on square spaces, so for 3D, the x, y, z coordinates all have the same region of 0->
maxValue. I should note these positions are integer values. In other words, from(0,0,0)to say(10,10,10)and any number combination in between.– TimeTravelPenguin
Nov 22 '18 at 13:15
What would be the point of such a table? With it, you could (in this case) look up
array[i, j, k]for anyi, j, k, but that would just give you what you used to look it up.– 500 - Internal Server Error
Nov 22 '18 at 13:15