c - 在公式中递增数组

标签 c arrays dynamic-arrays

我的程序旨在使用最小二乘法计算给定数组“输入”的线性回归系数。我的问题是在尝试计算数据的斜率时。如您所见,数组“输入”是一个 2 行数组。我能够将两行分开为输入 [i] 和输入 [j]。但是为了找到斜率,我需要分别使用这两行并在方程中递增它们来求解 v。如何使用这两个数组来获取 v 的值?

注意:使用下图中所示的输入,v 和 be 应分别为 2.795 和 -3.891。

我的任务: http://i1306.photobucket.com/albums/s576/sammyr2011/assignment2question4_zps08a5464b.jpg

到目前为止我的代码:

/*
*   Program: linreg.c
*   A program that calculates the linear regression coefficients
*   v and b using the least squares method.
*/

#include <stdio.h>
#include "forStatLib.h"

double input[][10] ={{0, 5, 10, 15, 20, 25, 30 ,35, 40, 45}, {0, 13, 22, 37, 54, 62, 64,     100, 112, 126}};

// Use the sum += when inputting array into equation

int main() {
int first, second, i, j, k;
double v, b, t, y;

//grab 1st row of the array
for(i=0; input[0][i] ; i++) {
    input[i]; 
}

//grab 2nd row of the array
for(i=0; input[1][i]; i++) {
    input[j];
}

/*calculate the average of both rows, first row is t and second is y. 
Then assign the averages of each as t_a and y_a. */
double t_a, y_a;
t_a = mean(10, input[i]);
y_a = mean(10, input[j]);

//calculate v (slope), Maybe use two loops with one nested?
// i need to increment
v = (  - t_a)(  - y_a) / ( )


} 

最佳答案

进行这些更改,

t_a = mean(10, input[0]);
y_a = mean(10, input[1]);

//calculate v (slope), Maybe use two loops with one nested?
// i need to increment
v1=0;
v2=0;
for(i=0; i< 10; i++)
{
 v1+ = ( input[0][i] - t_a)*( input[1][i] - y_a);
}
for(i=0; i< 10; i++)
{
 v2=v2+( input[0][i] - t_a)*( input[0][i] - t_a);
}
v=v1/v2;          //use float or double data type

input[][] 是一个二维数组,因此使用 input[i][j] 形式访问元素。
我不知道你为什么用,

for(i=0; input[0][i] ; i++) {
    input[i]; 
}

//grab 2nd row of the array
for(i=0; input[1][i]; i++) {
    input[j];               // j not initialized       
}

如果要打印值,请使用 printf()

for(i=0; i<10 ; i++) {
        printf("%d",input[0][i]); 
    }

    //grab 2nd row of the array
    for(i=0; i<10; i++) {
        printf("%d",input[1][i]);
    }

关于c - 在公式中递增数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602266/

相关文章:

c - 使用 Eclipse 进行远程 C 调试

c - malloc 和其他相关函数

python - 在 matplotlib 中仅连接一段数字数组

c++ - 使用堆上的成员初始化类内的类

我们可以在定义之前调用函数吗?

arrays - 我应该如何使用 swift 字典数组来获取所有值?

javascript - 获取数组 VueJS 的最后一项

c - 如何在 C 中的同一结构中使用两个可变长度数组

c - 如何在C中将值存储在动态数组中

c - 如何创建Lua模块的dll