5

I have following jagged array of "dists"

int[][] dists = new int[][]
{
    new int[]{0,2,3,5,2},
    new int[]{2,0,1,3,5},
    new int[]{3,1,0,4,4},
    new int[]{5,3,4,0,2},
    new int[]{2,5,4,2,0}
};

now I want to create another jagged array named as finalDists when I remove the 3rd row and 3rd column from original array of dists. I mean I want finally have following jagged array:

int[][] finalDists = new int[][]
{
    new int[]{0,2,5,2},
    new int[]{2,0,3,5},
    new int[]{5,3,0,2},
    new int[]{2,5,2,0}
}; 

I am confused how to handle this issue, before all thanks for your help

1
  • 2
    Consider using existing solutions for mathematics and matrices. No need to reinvent the wheel, assuming that is a matrix with matrix logic applied to it. Commented Apr 19, 2015 at 10:29

3 Answers 3

8
int[][] finalDists = dists.Where((arr, i)=>i!=2) //skip row#3
                          .Select(arr=>arr.Where((item,i)=>i!=2) //skip col#3
                                          .ToArray())
                          .ToArray();
Sign up to request clarification or add additional context in comments.

3 Comments

I had mistake, now your proposed solution works. :) Many thanks for your intimidate response.
I am new at Stackoverflow, I don't know how to accept and vote :)
Can you help for this probelm: [link] (stackoverflow.com/questions/29875959/…) thanks
1

Not very optimized but:

public static T[] RemoveRow<T>(T[] array, int row)
{
    T[] array2 = new T[array.Length - 1];

    Array.Copy(array, 0, array2, 0, row);
    Array.Copy(array, row + 1, array2, row, array2.Length - row);

    return array2;
}

public static T[][] RemoveColumn<T>(T[][] array, int column)
{
    T[][] array2 = new T[array.Length][];

    for (int i = 0; i < array.Length; i++)
    {
        array2[i] = RemoveRow(array[i], column);
    }

    return array2;
}

and

int[][] dists = new int[][]
{
    new int[]{0,2,3,5,2},
    new int[]{2,0,1,3,5},
    new int[]{3,1,0,4,4},
    new int[]{5,3,4,0,2},
    new int[]{2,5,4,2,0}
};

int[][] dists2 = RemoveColumn(RemoveRow(dists, 2), 2);

Note that you want to remove the third row and the third column, but their index is 2, because .NET arrays are 0-based indexed!

Comments

0

I tested all of the solutions on stack overflow on 3/3/2025 and this one was fastest on my test case.

static T[][] removeRowsAndColumns<T>(T[][] inputArray, List<int> rowsToRemove, List<int> columnsToRemove)
{
    T[][] removedRowsAndColumnsArray = new T[inputArray.Length - rowsToRemove.Count][];
    int currentRowIndex = 0;
    for (int i = 0; i < inputArray.Length; i++)
    {
        if (!rowsToRemove.Contains(i))
        {
            //removedRowsArray[currentRowIndex] = inputArray[i];
            removedRowsAndColumnsArray[currentRowIndex] = new T[inputArray[i].Length - columnsToRemove.Count];
            int currentColIndex = 0;
            for (int j = 0; j < inputArray[i].Length; j++)
            {
                if (!columnsToRemove.Contains(j))
                {
                    removedRowsAndColumnsArray[currentRowIndex][currentColIndex] = inputArray[i][j];
                    currentColIndex++;
                }
            }
            currentRowIndex++;
        }
    }
    return removedRowsAndColumnsArray;
}

Here is an example

int[][] thingToTest =
{
    new int[ ] { 10,  20,  30, 5},
    new int[ ] { 11,  22,   3, 4},
    new int[ ] { 88,  99,   1, 2},
    new int[ ] {111, 222, 333, 6}
};
List<int> rowsToRemove    = new List<int>() {2};
List<int> columnsToRemove = new List<int>() {2};


int[][] arrayWithRemovedColumnsAndRows = removeRowsAndColumns<int>(thingToTest, rowsToRemove, columnsToRemove);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.