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.

AttributesSimilarity5a.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "mex.h"
  2. void attributesSimilarity5a(double* nodeAtts,
  3. mwSize numNodes, mwSize numAttrs, double threshold,
  4. double* idxNodesToKeep, mwSize numNodesToKeep,
  5. mwIndex* ir_out, mwIndex* jc_out, double* outSimAtts)
  6. {
  7. double total=0, common=0, affinity=0;
  8. int nnz = 0;
  9. int prevCol = -1;
  10. int currCol, currRow, i, j, k;
  11. int a, a1, a2, a3;
  12. // printf("attributesSimilarity5a: nnz = %d\n", nnz_in);
  13. printf("start attributesSimilarity5a ... \n");
  14. for (j=0; j<numNodesToKeep; j++){
  15. currCol = (int)idxNodesToKeep[j]-1;
  16. for (k=prevCol+1; k<=currCol; k++){
  17. jc_out[k] = nnz;
  18. }
  19. for (i=0; i<numNodesToKeep; i++) {
  20. currRow = (int)idxNodesToKeep[i]-1;
  21. affinity = 0;
  22. if (currCol == currRow){
  23. affinity = 1;
  24. } else {
  25. affinity = 0;
  26. total = 0;
  27. common = 0;
  28. for (a=0; a<numAttrs; a++){
  29. a1 = (int)nodeAtts[a*numNodes+currCol];
  30. a2 = (int)nodeAtts[a*numNodes+currRow];
  31. a3 = a1+a2;
  32. if (a3 == 1) {total++;}
  33. if (a3 == 2) {total++; common++;}
  34. // if (a1==1 || a2==1) {
  35. // total++;
  36. // if (a1==a2) common++;
  37. // }
  38. }
  39. if (total > 0) affinity=common/total;
  40. }
  41. if (affinity > threshold) {
  42. // printf("attributesSimilarity5a [j=%d,i=%d][col=%d,row=%d]\n",j,i,currCol,currRow);
  43. outSimAtts[nnz] = affinity;
  44. ir_out[nnz] = currRow;
  45. nnz++;
  46. if (nnz % 1000 == 0)
  47. printf("attributesSimilarity5a nnz=%d K\n", nnz/1000);
  48. }
  49. }
  50. prevCol = currCol;
  51. }
  52. for (k=prevCol+1; k<=numNodes; k++){
  53. jc_out[k] = nnz;
  54. }
  55. printf("end attributesSimilarity5a => nnz=%d\n", nnz);
  56. }
  57. /* The gateway function
  58. //this function accepts four parameters:
  59. //1. an attributes matrix
  60. //2. an array with idxNodesToKeep
  61. //3. min threshold
  62. */
  63. void mexFunction( int nlhs, mxArray *plhs[],
  64. int nrhs, const mxArray *prhs[])
  65. {
  66. /* variable declarations here */
  67. mwSize numNodes, numAttrs;
  68. double* nodeAtts;
  69. double* idxNodesToKeep;
  70. double threshold;
  71. mwSize numNodesToKeep, estimateNNZ;
  72. double percent_sparse = 0.33;
  73. // ouput
  74. mwIndex* ir_out;
  75. mwIndex* jc_out;
  76. double* outSimAtts;
  77. printf("nlhs = %d, nrhs = %d\n", nlhs, nrhs);
  78. /* Check for the correct number of outputs */
  79. if(nlhs != 1){
  80. mexErrMsgIdAndTxt( "MATLAB:attributesSimilarity:wrongrhs",
  81. "Wrong number of output arguments.");
  82. }
  83. /* Check for the correct number of inputs */
  84. if(nrhs != 3){
  85. mexErrMsgIdAndTxt( "MATLAB:attributesSimilarity:wrongrhs",
  86. "Wrong number of input arguments.");
  87. }
  88. nodeAtts = mxGetPr(prhs[0]); /* pointer to first input */
  89. numNodes = mxGetM(prhs[0]); // rows
  90. numAttrs = mxGetN(prhs[0]); // cols
  91. idxNodesToKeep = mxGetPr(prhs[1]); /* pointer to second input matrix */
  92. numNodesToKeep = mxGetM(prhs[1]);
  93. // *** estimateNNZ as third of population
  94. estimateNNZ = numNodesToKeep*numNodesToKeep*percent_sparse;
  95. threshold = mxGetScalar(prhs[2]); /* pointer to thrid input matrix */
  96. printf("nodeAtts dimensions: numNodes=%d, numAttrs=%d\n", numNodes, numAttrs);
  97. printf("params: threshold = %f, numNodesToKeep= %d, estNNZ = %d\n", threshold, numNodesToKeep, estimateNNZ);
  98. // prepare output: nxn
  99. // allocate memeory according to estimateNNZ
  100. plhs[0] = mxCreateSparse(numNodes, numNodes, estimateNNZ, mxREAL);
  101. outSimAtts = mxGetPr(plhs[0]);
  102. ir_out = mxGetIr(plhs[0]);
  103. jc_out = mxGetJc(plhs[0]);
  104. // calculate the affinity
  105. attributesSimilarity5a(nodeAtts, numNodes, numAttrs, threshold, idxNodesToKeep, numNodesToKeep, ir_out, jc_out, outSimAtts);
  106. }