首頁 >web前端 >js教程 >javascript自然分類法演算法實作程式碼_javascript技巧

javascript自然分類法演算法實作程式碼_javascript技巧

WBOY
WBOY原創
2016-05-16 17:20:221423瀏覽

複製程式碼 程式碼如下:





🎜>


content="initial-scale=1, maximum-scale=1,user-scalable=no">
自然分類法演算法
<script><BR>function getJenksBreaks(data,numclass) {<BR>function sortNumber(a,b)//在javascript裡,Array的sort方法,必須用這個函數,否則不是按數字大小排序<BR>{<BR>return a - b<BR>}<BR>// int numclass;var numdata = data.length;<P>data.sort(sortNumber); //先排序<BR> <BR>var mat1=new Array();var mat2=new Array();<P>var st=new Array();<BR> <BR>for (var j = 0; j <= numdata; j ){<BR>mat1[j]=new Array();<BR>mat2[j]=new Array();<BR>st[j ]=0;<BR>for(var i=0;i<=numclass;i ){<BR>mat1[j][i]=0;<BR>mat2[j][i]=0;}<P>}<BR> <BR>for (var i = 1; i <= numclass; i ) {<BR>mat1[1][i] = 1;mat2[1][i] = 0;<P>for (var j = 2; j <= numdata; j ){<BR> <BR>mat2[j][i]=Number.MAX_VALUE;<BR>}}<P>var v=0;<BR> <BR>for (var l = 2; l <= numdata; l ) {<BR>var s1=0;<BR>var s2=0;<BR>var w=0;<BR>var i3=0; for (var m = 1; m <= l; m ) {<P>i3 = l - m 1; <P>var val=parseInt(data[i3-1]);<BR> s2 = val * val;<P>s1 = val;<BR> <BR>w ;<BR>v = s2 - (s1 * s1) / w;<BR>var i4 = i3 - 1;<BR>if (i4 != 0) {<BR>for (var j = 2 ; j <= numclass; j ) {<BR>if (mat2[l][j] >= (v mat2[i4][j - 1])) {mat1[l][j] = i3; <P>mat2[l][j] = v mat2[i4][j - 1];<BR> <BR>if(l==200&&j==5) alert("l=" 200 ",j=" 5 ";mat2[200][5]=" mat1[l][j] "i3=" i3) ;<BR>}<BR>}}<P>}<BR> <BR>mat1[l][1] = 1;mat2[l][1] = v;<P>}<BR> var k = numdata;<P>var kclass=new Array();<BR> <BR>/* int[] kclass = new int[numclass]; */kclass[numclass - 1] = parseInt(data[data.length-1]);<P>/* kclass[numclass - 1 ] = (Integer) data.get(data.size() - 1); */<BR> <BR>for (var j = numclass; j >= 2; j--) {<BR>var id = parseInt(mat1[k][j]) - 2;<BR>kclass[j - 2] = parseInt ( data[id]);k = parseInt( mat1[k][j] - 1);<P>}<BR> return kclass;<P>}<BR> <BR>function doit(){<BR>var data = new Array( 9, 9, 9, 9, 8, 1, 9, 7, 6, 7, 10, 7, 7, 8, 5, 7, 8 , 2,<BR>6, 6, 9, 10, 2, 2, 3, 9, 9, 1, 9, 1, 6, 7, 1, 9, 6, 5, 2, 8, 6,<BR>6, 6, 4, 1, 4, 10, 9, 6, 9, 9, 7, 10, 9, 6, 6, 6, 9, 3, 2, 5,<BR>10, 3, 9, 6, 1, 2, 1, 2, 7, 5, 2, 6, 6, 2, 7, 9, 9, 5, 4, 6, 4,<BR>1, 1, 5, 1, 4, 4 , 2, 6, 9, 7, 3, 10, 4, 4, 9, 5, 5, 3, 5, 3,<BR>161, 136, 22, 113, 48, 114, 166, 33, 159, 182, 135, 22, 174,<BR>191, 60, 176, 1, 155, 62, 142, 185, 84, 140, 27, 111, 176, 189, 🎜> 20, 70, 172, 90, 120, 121, 173, 57, 138, 86,<BR>180, 143, 155, 35, 10, 136, 158, 26, 29, 136, 199, 1939, 1939, 1939, 1939, 193, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 1993, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 1939, 19393, 1939, 195 🎜>104, 147, 182, 44, 84, 139, 176, 16, 130, 168, 109, 182, 87,<BR>78, 115, 75, 32, 87,<BR>78, 115, 75, 32, 87,<BR>78, 115, 75, 32, 18, 689, 18, 6895, 6 , 60, 72, 92, 129,159, 99, 5, 192, 6, 53, 15, 62, 176, 73, 129, 128, 34, 190, 95,<P> , 30 );<BR> <BR>/* var list = new Array();<BR>for(int i = 0; i < data.length; i ){<BR>list.add(data[i]);<BR>} * /<BR>//Collections.sort(list);<BR>var text=document.getElementById("text");<BR>text.value="";<BR>var grade = getJenksBreaks(data, 5) ;for (var i = 0; i < grade.length; i ) <P>text.value=text.value grade[i] ","; <P>}<BR> </script>





陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn