I'm converting a matlab code to C++ code and trying to find a function in Lapack, Blas or even Atlas that does the same job at Diag(k) function in matlab ? Any suggestions ?
Asked
Active
Viewed 1,133 times
0
-
"`Diag(k)`"? Do you mean, creation of a diagonal matrix from a vector or extraction of the diagonal of any matrix as a vector? – leftaroundabout Apr 12 '12 at 18:49
-
... and the answer depends on how you represent matrices and vector in C++. In particular, what is the type of `k`? Either way, you don't need Blas or Lapack (ATLAS is just an implementation of these) for this, as all it is a simple looping over an index : `for ( i=0; i
– sly Apr 12 '12 at 20:15
2 Answers
0
GSL, the Gnu Scientific Library has a function gsl_matrix_diagonal (gsl_matrix * m)
. This creates a "vector view" into the matrix which is more or less a gsl vector aliased to the diagonal of the matrix. Dunno if you love GSL, but it also includes BLAS support. I think it's a very good library.

Michael Daum
- 820
- 6
- 12
0
static void create_diagonal_matrix(T* matrix,T* v,int size)
{
for(int i=0;i<size;i++)
{
for(int32_t j=0;j<size;j++)
{
if(i==j)
matrix[j*size+i]=v[i];
else
matrix[j*size+i]=0;
}
}
}

Mohamed Taher Alrefaie
- 15,698
- 9
- 48
- 66