Amoiensis / Matrix_hub

A lib of Matrix operation for C language. (矩阵运算库--C语言)
Apache License 2.0
234 stars 53 forks source link

写了个根据向量生成对角矩阵的函数 #19

Open Fixer-itb opened 10 months ago

Fixer-itb commented 10 months ago

Matrix Matrix_asDiagonal(MATRIX_TYPE data,int dimension) { Matrix mat_ = (Matrix)malloc(sizeof(Matrix)); if (mat == NULL) { printf("malloc space failed\n"); return NULL; } int size = dimension * dimension; mat->row = dimension; mat_->column = dimension;

mat_->data = (MATRIX_TYPE*)malloc((size) * sizeof(MATRIX_TYPE));
memset(mat_->data, 0, sizeof(MATRIX_TYPE) * size);

for(int i = 0;i<dimension;i++){
    for(int j =0;j<dimension;j++){
        if(i==j){
            mat_->data[i*dimension+j] = data[i];
        }
    }
}
return mat_;

}