You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AttributesSimilarity.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "mex.h"
  2. void attributesSimilarity(mwIndex* ir, mwIndex* jc, double* commonAtts,
  3. double *numAtts, mwSize n, double threshold,
  4. mwIndex* ir_out, mwIndex* jc_out, double* outSimAtts)
  5. {
  6. double numTotalAtts, numCommon;
  7. int currCol = 0, startIdx, stopIdx, i, j, currRow;
  8. int nnz_in = jc[n];
  9. int nnz_out = 0;
  10. printf("attributesSimilarity: nnz = %d\n", nnz_in);
  11. for (currCol = 0; currCol < n; currCol++)
  12. {
  13. jc_out[currCol] = nnz_out;
  14. startIdx = jc[currCol];
  15. stopIdx = jc[currCol + 1];
  16. for(i = startIdx; i < stopIdx; i++)
  17. {
  18. currRow = ir[i];
  19. numCommon = commonAtts[i];
  20. numTotalAtts = numAtts[currRow]+numAtts[currCol]-numCommon;
  21. // // debugging
  22. // if (currCol<30 && currRow<30) {
  23. // printf("(i,j)=(%d,%d): common=%f, total=%f\n", currRow,currCol,numCommon,numTotalAtts);
  24. // }
  25. if (numTotalAtts > 0 && numCommon/numTotalAtts > threshold)
  26. {
  27. outSimAtts[nnz_out] = numCommon/numTotalAtts;
  28. ir_out[nnz_out] = ir[i];
  29. nnz_out++;
  30. // } else
  31. // {
  32. // outSimAtts[i] = 0;
  33. // ir_out[i] = ir[i];
  34. }
  35. }
  36. }
  37. jc_out[n] = nnz_out;
  38. }
  39. /* The gateway function
  40. //this function accepts two parameters:
  41. //1. a sparse affinity matrix (should be the number of common attributes between nodes i and j
  42. //2. an array with the number of attributes of each node
  43. */
  44. void mexFunction( int nlhs, mxArray *plhs[],
  45. int nrhs, const mxArray *prhs[])
  46. {
  47. /* variable declarations here */
  48. mwIndex* ir;
  49. mwIndex* jc;
  50. double* commonAtts;
  51. double* numAtts;
  52. mwSize n,m,n2,m2;
  53. double threshold;
  54. mwIndex* ir_out;
  55. mwIndex* jc_out;
  56. double* outSimAtts;
  57. int i;
  58. printf("nlhs = %d, nrhs = %d\n", nlhs, nrhs);
  59. /* Check for the correct number of inputs */
  60. if(nrhs != 3){
  61. mexErrMsgIdAndTxt( "MATLAB:attributesSimilarity:wrongrhs",
  62. "Wrong number of input arguments.");
  63. }
  64. commonAtts = mxGetPr(prhs[0]); /* pointer to first input */
  65. ir = mxGetIr(prhs[0]);
  66. jc = mxGetJc(prhs[0]);
  67. numAtts = mxGetPr(prhs[1]); /* pointer to second input */
  68. threshold = mxGetScalar(prhs[2]); /* pointer to thrid input matrix */
  69. printf("threshold = %f\n", threshold);
  70. /* dimensions of input matrices */
  71. m = mxGetM(prhs[0]);
  72. n = mxGetN(prhs[0]);
  73. m2 = mxGetM(prhs[1]);
  74. n2 = mxGetN(prhs[1]);
  75. printf("dimensions: m=%d, n=%d, m2=%d, n2=%d\n", m, n, m2, n2);
  76. if (m != m2) {
  77. mexErrMsgIdAndTxt("MATLAB:attributesSimilarity:matchdims",
  78. "Inner dimensions of matrices do not match.");
  79. }
  80. if (n != m) {
  81. mexErrMsgIdAndTxt("MATLAB:attributesSimilarity:square",
  82. "Function requires input matrix 1 must be square.");
  83. }
  84. if (n2 != 1) {
  85. mexErrMsgIdAndTxt("MATLAB:attributesSimilarity:zerodivide",
  86. "Function requires input matrix 2 must be a column vector.");
  87. }
  88. plhs[0] = mxCreateSparse(n,n, jc[n], mxREAL);
  89. outSimAtts = mxGetPr(plhs[0]);
  90. ir_out = mxGetIr(plhs[0]);
  91. jc_out = mxGetJc(plhs[0]);
  92. attributesSimilarity(ir, jc, commonAtts, numAtts, n, threshold, ir_out, jc_out, outSimAtts);
  93. }