c# 取中值


 說明:  定義一個10位數的數組,接受穿過來的5 ,就是原數組地1--1共5位 結束,然后取中間值,然后依次循環1.12--4,再去中間值,然后將新拿到的中間值,將原有數組 第5+1 位替換掉,生成一個新數組。 就這樣:
1, 1.12, 0.9, 1.2, 1, 4, 1.1, 6, 0.9, 4         原數組
1, 1.12, 0.9, 1.2, 1,1,1.12,1.1,1.2,1.1       新數組





//首次從0開始,數組的首標自動+1; int zxh = 0; // 循環完一次后數組的尾標+1 int last = 0; //List<string> jstring = new List<string>; List<double> zws = new List<double>(); using (var db = new SecureCloud_Entities()) { //原數組 double[] myarray = { 1, 1.12, 0.9, 1.2, 1, 4, 1.1, 6, 0.9, 4 }; double[] newmyarray = new double[myarray.Length]; myarray.CopyTo(newmyarray,0); int length = myarray.Length; int a = 5; //傳過來的值,到第幾位結束 //最終循環次數 int cycles = length - a; List<double> windowPixelColorList = new List<double>(); double med; // int zzxh = length - a + 1; for (int i = zxh; i <= myarray.Length; i++) { if (zxh < cycles) { if (i <= a - 1 + last) // && { windowPixelColorList.Add(myarray[i]); windowPixelColorList.Sort(); //數組從0開始,滿足6時進入取中位數 if (a - i + last == 1) { i = zxh; double[] list = windowPixelColorList.ToArray(); int count = list.Length; //判斷中位數是偶數 if (count % 2 == 0) { med = (list[count / 2] + list[count / 2 - 1]) / 2d; } else { //中位數奇數 med = list[count / 2]; } // newmyarray[a + last] = med; zws.Add(med); zxh++; windowPixelColorList = new List<double>(); last++; } } } else { break; } } Console.WriteLine(" {0}", zws.ToString()); //原來的數據 Console.WriteLine(" {0}", newmyarray.ToString()); //改變后的數據 } return ;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM