I'm having trouble dividing two elements of an array.... it seems to work in some instances, but not right now... I have two matrices[row][col] sized and am trying to transpose the elements to a matrix to a vector/array. That all works fine... but when I then try to divide the elements by a ratio it all buckles down....

double total[col];
  double ratio[col];
  
  for(i=0;i<col;i++){
    total[i]=0;
    ratio[i]=0; }
  
  for(i=0; i<col; i++){
    for(j=0;j<row;j++){
      total[i]=total[i]+matrix[j][i];  		
    }
    printf("total %f\n",total[i]);
  }

....

  printf("min %f",min);
  // Ratio time...
  for(i=0;i<col;i++){
    ratio[i]=total[i]/min;
    printf("%f ratio \n",ratio[i]);
  }
  
  // Change matrix into an array as received by R for compatibility.
  c=0;
  for(i=0;i<row;i++){
    for(j=0;j<col;j++){
      matrix2[i][j]=matrix[i][j]; // Create a copy...
      printf("matrix %f \n",matrix2[i][j]);
      c++;
    }
  }
  
  c=0;
  for(i=0;i<col;i++){
    for(j=0;j<row;j++){
      pmatrix[c]=matrix2[j][i];
      printf("pmatrix %f \n",pmatrix[c]);
      c++;
    }
  }
 
  counter = 0;
  j=-1;
  for (i=0; i<size; i++) {
    if (counter % row == 0) {
      j++;
    }[B]
    pmatrix[i]=pmatrix[i]/ratio[j];[/B]
    printf("pmatrix %d after ratio: %f \n",i,pmatrix[i]);
    counter++; 
  }

sometimes, if I divide pmatrix by ratio [j] TWICE then it comes out with the correct value for dividing the first time...

The issue in particular is :
pmatrix=pmatrix/ratio[j];

When I do it twice, and row ==2, it works... when I leave it as is, and row == 13 it works fine....

Edited 6 Years Ago by nosson: clarity

j=-1;
  for (i=0; i<size; i++) {
    if (counter % row == 0) {
      j++;
    }
    pmatrix[i]=pmatrix[i]/ratio[j];
    printf("pmatrix %d after ratio: %f \n",i,pmatrix[i]);
    counter++; 
  }

In the if(counter % row == 0) line, are you aware that when i== 0 (first time into the loop), that the statement will be TRUE, and j will be incremented? Looks like you have it covered with the assignment of j=-1, but I'm not sure.

Finally, you're printing a double, as if it were a float, using %f. You should be using %lf, instead.

Edited 6 Years Ago by Adak: n/a

Thanks, yeah - I placed j=-1 to cover that...

Also, I'm just using the printfs for debugging, but I switched them - thanks for the tip. No changes for output though...

So, it appears that whenever I have rows too similar, the line:

pmatrix[i]=pmatrix[i]/ratio[j];

doesn't register to the proper value... I am very perplexed... :-(

Edited 6 Years Ago by nosson: n/a

Wow... I am a bit embarrased. Seems that the code was working all along, I just imagined it wasn't....


Thanks Adak for the help!

This article has been dead for over six months. Start a new discussion instead.