Quote:
Originally Posted by PB0711
Code:
for(int i=0: i > numRows; i++){
for(int j=0; j> numRows; j++){
if(VectorCriteria[i] > kon+VectorCriteria[j]){
do correlation mat[i][ ] vs mat[j][ ]
}
}
}
|
This will be very inefficient. Using R code, the correlation matrix, where element (m,n) is the correlation coefficient between row m and row n of the original matrix, can be computed as follows:
Code:
vals <- #the matrix you described
vals.covar <- (function(x) x %*% t(x) / ncol(x))(vals) #the covariance
vals.corr <- (function(x) diag( 1/sqrt(diag(x)) ) %*% x %*% diag( 1/sqrt(diag(x)) ))(vals.covar) #the correlation matrix
You can break this down into matrix multiplication (
%*% R operator) in C, and since
vals.covar and
vals.corr are by definition symmetric you only need to compute half the elements. The computation of
vals.corr is just a normalization of each row and column using the square-root of the corresponding diagonal element, leaving the diagonal elements equal to 1.
Quote:
Originally Posted by PB0711
I would like to interface my R program with some C code, but first I need the C code.
|
If you're going to write an R extension in C you'll need to write a wrapper in R, anyway. You might as well design it so you only compute in C what can't be adequately computed in R, e.g. compute the correlation in R and pass it to a C function that expects it to already be computed.
Kevin Barry