从数组对象

时间:2016-10-30 11:57:45

标签: c# arrays double row

从下面double array中删除多行是否有更好方法?

double[] RetDist;

HashSet<int> rowsToRemove = new HashSet<int> { 249, 250, 251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,266,267,268,269};

RetDist = dataDist.Where((source, index) =>  !rowsToRemove.Contains(index)).ToArray();

主要关注:假设我要删除1000行(极端情况),手动指定效率可能非常低: < / p>

HashSet<int> rowsToRemove = new HashSet<int> { 249, 250,...};

编辑 - 到目前为止使用方式:

  1. array[]转换为List<>

    List<double> Lretdist = RetDist.ToList();
    
  2. 通过indexes删除系列method我最初想要避免

    List<double> test = RemoveIndexRowFromList(Lretdist, "m");
    
      public static List<double> RemoveIndexRowFromList(List<double> lst, string  obsfrequency)
    {
        int idx, totalrow, endindex, del;    
        totalrow = lst.Count;
    
        switch (obsfrequency)
        {
            case "d":
    
                idx = 1;
                endindex = totalrow - idx;
    
                while (endindex < totalrow)
                {
                    del = totalrow - 1;
                    lst.RemoveAt(del);
                    totalrow = lst.Count;
                }
    
                break;
    
            case "m":
    
                idx = 20;
                endindex = totalrow - idx;
    
                while (endindex < totalrow)
                {
                    del = totalrow - 1;
                    lst.RemoveAt(del);
                    totalrow = lst.Count ;
                }
    
                break;
        }
    
        return lst;
    }
    
  3. 切换回array[]

    double[] Re = test.ToArray(); // and keep remaining computations using arrays
    
  4. Ps 我承认这3个额外步骤可能有点sub-optimal但是它 我想它比hard-coding要删除的行数要好一些。

0 个答案:

没有答案