Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, May 28, 2015 10:05 AM
Hello,
I wish to delete several columns from a large multidimensional (2) array into a smaller array (I realize that I cannot simply delete columns in a MD Array and must copy it into a new one). Now I have searched all over the net for example code to do this but several I have tried have all had bugs in them. Has anyone got a working example of how I can reduce (delete) 1 or more columns from a multidimensional Array? I have tried I think nearly all examples currently available on the Net for this and None of them work properly.
All replies (5)
Thursday, May 28, 2015 11:01 AM ✅Answered | 1 vote
Hi,
You can use this method to select columns from two dimensional array
private T[,] ResizeArray<T>(T[,] original, int[] indices)
{
var newArray = new T[original.GetLength(0), indices.Length];
int minRows = original.GetLength(0);
int minCols = Math.Min(indices.Length, original.GetLength(1));
for (int i = 0; i < minRows; i++)
{
for (int j = 0; j < minCols; j++)
newArray[i, j] = original[i, indices[j]];
}
return newArray;
}
lets say you have this array
string[,] values = new string[,] { { "item11", "item12", "item13" }, { "item21", "item22", "item23" } };
and you need to remove the middle column
int[] indices = new int[] { 0, 2 };
string[,] newArray = ResizeArray<string>(values, indices);
If this answer was helpful please remember to close your threads by marking helpful posts as answer
Fares
Thursday, May 28, 2015 12:18 PM ✅Answered | 1 vote
If I understand your problem, I have modified the method to get the indices of the columns those you need to remove
private T[,] ResizeArray<T>(T[,] original, int[] columnsToRemove)
{
var newArray = new T[original.GetLength(0), original.GetLength(1)-columnsToRemove.Length];
int minRows = original.GetLength(0);
int minCols = Math.Min(columnsToRemove.Length, original.GetLength(1));
for (int i = 0; i < minRows; i++)
{
int currentColumn = 0;
for (int j = 0; j < original.GetLength(1); j++)
{
if (columnsToRemove.Contains(j) == false)
{
newArray[i, currentColumn] = original[i, j];
currentColumn++;
}
}
}
return newArray;
}
string[,] values = new string[,] { { "item11", "item12", "item13" }, { "item21", "item22", "item23" } };
int[] indices = new int[] { 1 };
string[,] newArray = ResizeArray<string>(values, indices);
this will remove the column 1
Thursday, May 28, 2015 12:24 PM ✅Answered
Thanks very much Fares - your addition looks very good! I will give it a try in the next hour when I am finished with something else (pretty urgent) I am doing.
Thursday, May 28, 2015 11:55 AM
Hallo Fares, thank you for your reply. It is quite helpful, but I have a large amount of columns and they are dynamic as well, so the number of columns can obviously vary and I won't know which column to delete until I trap it at run time. For example I have 57 columns in the 2D Array but I want to delete column 9, or even better (and which I really need) is to delete columns 9-14. Once I have found/know the left most column I want to delete that column and 4 or 5 more over to its right. Cany your code be adapted to fit the above problem? Or is there another way of doing it? I already have the 2D Array in an Array in this Format - arrCalculations[,] with thousands of rows and usually about 60 columns. (Of course this is easy in Excel which is where the array will eventually land, but the people I am working for want it all in C# for maintenance purposes as they don't have Excel programmers.) (Thank you very much for your help.)
Thursday, May 28, 2015 1:56 PM
Fares - has worked wonderfully! Thanks very much! First class coding and help.