[英]How to calculate the standard deviation for an array column in Java?
Here is an array:这是一个数组:
int[][] array1 = {
{1,2},
{3,4},
{5,6},
{7,8},
{9,10}
};
My goal is to use the array above to find the standard deviation for the column:我的目标是使用上面的数组来查找列的标准偏差:
public double stdevCol(int[][] array, int col)
{
}
Here is my standard deviation function for the total:这是我对总数的标准偏差函数:
public double stdev2D(int[][] array)
{
int counter =0;
double mean = mean2D(array);
double sumOfSq =0;
for(int i = 0; i<array.length; i++)
{
for(int j = 0 ; j< array[i].length; j++)
{
sumOfSq += Math.pow(array[i][j] - mean, 2.0);
counter++;
}
}
return Math.sqrt(sumOfSq/counter);
}
My question: If using the total as a reference point, would I switch instead of array[i].length
to (array,col).length
?我的问题:如果使用总数作为参考点,我会从array[i].length
切换到(array,col).length
吗?
Experimental code实验代码
{
int counter =0;
double mean = meanCol(array);
double sumOfSq =0;
for(int i = 0; i<array[col].length; i++)
{
sumOfSq += Math.pow(array[col].length - mean, 2.0);
counter++;
}
return Math.sqrt(sumOfSq/counter);
}
import java.io.*;
class GFG {
// Function for calculating
// variance
static double variance(double a[], int n) {
// Compute mean (average of elements)
double sum = 0;
for (int i = 0; i < n; i++){
sum += a[i];
}
double mean = (double)sum /(double)n;
// Compute sum squared differences with mean.
double sqDiff = 0;
for (int i = 0; i < n; i++){
sqDiff += (a[i] - mean) *(a[i] - mean);
}
return (double)sqDiff / n;
}
static double standardDeviation(double arr[], int n) {
return Math.sqrt(variance(arr, n));
}
// Driver Code
public static void main (String[] args) {
double arr[] = {600, 470, 170, 430, 300};
int n = arr.length;
System.out.println( "Variance: " + variance(arr, n));
System.out.println ("Standard Deviation: " + standardDeviation(arr, n));
}
}
All credits go to vt_m of course, I have just changed his formatting当然,所有学分都归 vt_m,我刚刚更改了他的格式
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.