How do you remove unwanted data in a 2D matrix?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jif20
    New Member
    • Dec 2007
    • 2

    How do you remove unwanted data in a 2D matrix?

    Hello,

    basically i have a 2D matrix 'a'. It contains both data i want to use and data i want to ignore.

    e.g.

    'a' matrix:

    1.937145 -0.500000 -1.333333 0.000000 -0.103812 -1.000000 0.000000

    -0.500000 1.000000 0.000000 0.000000 0.000000 -3.000000 0.000000

    -1.333333 0.000000 1.333333 0.000000 0.000000 -2.000000 0.000000

    0.000000 0.000000 0.000000 0.535115 -0.285115 -3.000000 0.000000

    -0.103812 0.000000 0.000000 -0.285115 0.388927 0.000000 0.000000

    0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000

    The data i want to extract and save into another array is surrounded by extra rows and columns full of zeros. I want to save the data (minus the zeros) into another array, lets call that 'A'.

    after much head scratching i cannot get this to work. can anyone do this for me?

    thank you, James
  • jif20
    New Member
    • Dec 2007
    • 2

    #2
    i forgot to mention, in order to produce this array, malloc has been used. so it has an unknown amount of columns filled with zeros.

    Comment

    • Andr3w
      New Member
      • Nov 2007
      • 42

      #3
      Hi please could you clarify a bit what you mean when you say:
      "is surrounded by extra rows and columns full of zeros"

      But if this is of any use to truncate the zeros from one float value (I assume from the accurancy you have on the numbers on that matrix that they are floats) you could use the following function, it's simple and bulky but does the job well

      [code=c]

      float truncateZeros( float numA )
      {
      int frTemp, intTemp;
      int posCounter = 0;

      // call to modf
      frTemp = modf( numA, &intTemp );

      // check if the fraction part is 0L
      while( frTemp != 0L )
      {
      // recheck but after we multiply our number with 10
      numA = numA*10;

      frTemp = modf( numA, &intTemp );

      // counter
      posCounter++
      }

      // return the final value without the zeros
      return ((numA)/(posCounter*10) );
      }
      [/code]

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        You can copy the elements of one array to another array using a for loop.
        e.g. For a single dimension array:
        Code:
        for(int i=0;i<20;i++)
        {   if(i==0.0)continue;//jumps to top of loop without copying 0.0
            b[i]=a[i];}
        I think you could use the same principle for a 2 dimensional array

        Comment

        Working...