Reputation: 1112
I have to store two types of information in any data structure for what I came up with the scrap solution of 2D array in C#. I have to store as:
int
data type int
data type If I use a 2D array as:
Int32[,] _clusterMembership = new Int32[10, 10];
But the issue here is:
So the question is: How can I manage to store this information in C# ?
ADDENDUM
I have to use answer from this question here in this method as:
public static List<Cluster> DP_Cluster(List<string> _customer, double _alpha)
{
var _currentClusters = 0; // current number of clusters i.e. "k"
var _memberNumber = 0; // running member number i.e. "n"
//var _dic = new Dictionary<int, List<string>>();
var _probOld = 0.0;
var _probNew = 0.0;
List<Cluster> myClusters = new List<Cluster>();
Cluster cluster = new Cluster(_currentClusters += 1);
cluster.Members.Add(new ClusterMember { Name = _customer.ElementAt(_memberNumber) });
myClusters.Add(cluster);
//_dic.Add(_currentClusters, _customer.ElementAt(_memberNumber));
_currentClusters += 1;
for(int _i = 1; _i < _customer.Count - 1; _i++)
{
if( _i <= _currentClusters)
{
_probOld = myClusters[_i].Members.Count / ((_i+1) - 1 + _alpha);
}
else
{
_probNew = _alpha / ((_i+1) - 1 + _alpha);
}
if(_probNew > _probOld)
{
// Add _customer.ElementAt(_memberNumber+=1) to New Cluster
Cluster cluster = new Cluster( _currentClusters += 1 ); // Here is an error as we defining same name for another cluster
myClusters.Add(cluster);
}
else
{
// Add _customer.ElementAt(_memberNumber+=1) to Old Cluster
}
}
return myClusters;
}
Upvotes: 3
Views: 3572
Reputation: 43876
You should consider making two types, one for the clusters and one for the members:
Members
public class ClusterMember
{
public string Name {get;set;}
// more properties...
}
Clusters
public class Cluster
{
public int ID {get;}
public List<ClusterMember> Members {get;}
public Cluster(int id)
{
ID = id;
Members = new List<ClusterMember();
}
}
And then you can store your clusters in a list
List<Cluster> myClusters = new List<Cluster>();
Cluster cluster = new Cluster(1);
cluster.Members.Add(new ClusterMember { Name = "Member1" });
myClusters.Add(cluster);
UPDATE I assumed that you want to do more with your data than just store these two information and tried to provide a better object-oriented approach.
To get your counts:
int totalNumberOfClusters = myClusters.Count;
int numberOfMembersInOneCluster = cluster.Members.Count;
int totalNumberOfClusterMembers = myClusters.Sum(c => c.Members.Count);
And to output the number of members for each cluster:
foreach(Cluster c in myClusters)
Console.WriteLine($"Cluster {c.ID} has {c.Members.Count} members.");
Upvotes: 2
Reputation: 993
static void Main(string[] args)
{
var clusterMembership = new Dictionary<int, int>();
//Add cluster 123 and assign a member count of 4
clusterMembership.Add(123, 4);
//Change member count for cluster 123 to 5
clusterMembership[123] = 5;
//Remove cluster 123
clusterMembership.Remove(123);
//Get the number of clusters in the dictionary
var count = clusterMembership.Count;
//Iterate through the dictionary
foreach(var clusterKey in clusterMembership.Keys)
{
var memberCount = clusterMembership[clusterKey];
}
}
Upvotes: 0
Reputation: 2446
As is already mentioned. You can simply use a list. The bottom code example shows how to create the list type you need and how you add and access values in that list.
using System.IO;
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
//Creating a list of lists that contains integers
List<List<int>> clusters = new List<List<int>>();
//each list in the above list consists of a list of integers. So we need to add list of integers to that list
List<int> row = new List<int>();
//now we add integers to the list
row.Add(1); row.Add(2); row.Add(3); row.Add(4);
//Now we add the list of integers to the list of lists of integers
clusters.Add(row);
foreach(List<int> rows in clusters)
{
foreach(int num in rows)
{
System.Console.WriteLine(num);
}
}
Console.WriteLine("number of rows: {0}", clusters.Count);
Console.WriteLine("number of elements in the first row: {0}", clusters[0].Count);
}
}
Upvotes: 2
Reputation: 1416
You can store your data in a list which has keyvaluepair items. Or use Dictionary
List<KeyValuePair<int, int>>()
Dictionary<int,int>();
so you can add new keyvaluepair for each cluster.
Upvotes: 0
Reputation: 367
You could consider using a list of lists;
List<List<int>> clusters;
See this answer on another question for more info and also how to make it into a more generic class: https://stackoverflow.com/a/1596563/6065552
Upvotes: 0