Grouping neighbouring entries with the same value in a 2d array
Grouping neighbouring entries with the same value in a 2d array
EDIT: I now realized the question was not appropriate for stack but I've gotten a lot of helpful advice anyway. Thanks everyone!
I have a 2d array and I want to group together neighbors of the same value. Using C# (working with unity).
Let's say I have this:
int[,] array {
0,0,0,0,0,0,1,0,0,0,
0,1,1,0,0,0,1,0,0,0,
0,1,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,1,1,1,0,
0,0,0,0,0,0,1,1,1,0
}
There are three "clusters" of 1:s. I want to add them to a dictionary with some variable for identification. So maybe first add the neighboring values to a list, add that list to a dictionary, clear the list and move onto the next cluster.
The columns and rows would be of equal length in the real thing.
I would also want the sorting method to accept arrays of various sizes so no hardcoded values. I parse the array from an XML document.
I've tried looking into Array.Sort
but the resources I have found have been exclusively about sorting values in as/descending order. Just pointing me in the right direction, some some relevant web resources would be greatly appreciated!
Array.Sort
I think my question is pretty specific. If nothing else i would appreciate "putting you in the right direction". But im sorry if i have misinterpreted the rules.
– Stahhl
Jun 29 at 15:02
There are no built-ins that would identify the features you're looking for in a datastructure like this. You need to write your own. Make an attempt at doing that and we can help you solve issues with it.
– Draco18s
Jun 29 at 15:31
You need to break this into smaller sub-problems. For example, how do you find "clusters" in the array? How do you represent the clusters in your dictionary? How do you label the clusters for identification? These last two questions only you can answer since you are building this particular software. The first question will take some research to find existing algorithms that you can adapt to your problem.
– Code-Apprentice
Jun 29 at 16:10
1 Answer
1
I'm not going to give you the answer in full code since 1. you shouldn't be asking for it here and 2. you can definitely work it out yourself.
This is a good opportunity for you to whip out your pen and paper and figure out the algorithm. Lets say we want something similar to your task: just grouping the clusters of ones. The pseudocode might look like this.
If would then run through this on paper with a small example.
Once you have your desired algorithm, putting it into a dictionary and sorting it should be trivial.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Stack isn´t the right place for "putting you in the right direction". Instead it is for specific questions on specific programming-related problems. Asking "how do I do this" is therefor off-topic here.
– HimBromBeere
Jun 29 at 14:54