How to save some columns and rows of a jagged array and remove unnecessary columns and rows in C #

I have the following jammed array

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

and I found out how to delete one specific column and row (e.g. 3). Here is the link .

Now I want to have dynamic programming as follows: consider this array

  int[] day={1,4,5}; 

this array may be another array ( I used a dynamic term for this reason )

where the elements of the "day" array show the rows and columns of the "dists1" matrix, so I want the new gear array named "dists2" to contain only columns and rows 1,4,5 with the row and column "0" because it is fixed row and column as below:

  int[][] dists2 = new int[][] { new int[]{0,2,2,4}, new int[]{2,0,5,3}, new int[]{2,5,0,2}, new int[]{4,3,2,0} }; 
+1
source share
1 answer

Just change the answer and replace the condition i! = 2 with the method Contains an array of days:

 int[] day = {1,4,5}; int[][] finalDists = dists.Where((arr, i) => i == 0 || day.Contains(i)) //skip rows .Select(arr=> arr.Where((item, i) => i == 0 || day.Contains(i)) //skip cols .ToArray()) .ToArray(); 

In any case, if performance matters, it is better to use for loops instead of LINQ.

+2
source

All Articles