Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. how can i save 2D array of double to a file?

how can i save 2D array of double to a file?

Scheduled Pinned Locked Moved .NET (Core and Framework)
questiondata-structures
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marco M
    wrote on last edited by
    #1

    i tried to use FileStream Class but it only supports writing byte[] not even byte[,].. anyone has a clue ? thanx shakooosh

    I R 2 Replies Last reply
    0
    • M Marco M

      i tried to use FileStream Class but it only supports writing byte[] not even byte[,].. anyone has a clue ? thanx shakooosh

      I Offline
      I Offline
      ian mariano
      wrote on last edited by
      #2

      Suppose wr is your BinaryWriter, your multidimensional (rectangular) array, theArray is 2D. Store and write the dimensions out thus (C#):

      int mupper = theArray.GetUpperBound(0);
      int nupper = theArray.GetUpperBound(1);
       
      ws.Write(mupper);
      ws.Write(nupper);
      

      Then write out the bytes:

      for (int i = 0; i < mupper; i++)
      {
         for (int j = 0; j < nupper; i++)
            wr.Write(theArray[i, j]);
      }
      

      To read it back in, you do the reverse:

      //  rdr is a BinaryReader
      int mupper = rdr.ReadInt32();
      int nupper = rdr.ReadInt32();
      byte[,] theArray = new byte[mupper, nupper];
       
      for (int i = 0; i < mupper; i++)
      {
         for (int j = 0; j < nupper; i++)
            theArray[i, j] = rdr.ReadByte();
      }
      

      The problem is that this is not that efficient. If, however, you used a jagged array (actually, the secondary array never differs in length so for all purposes it's still rectangular not jagged,) you can define theArray 2d, m,n like this:

      byte[][]  theArray = new byte[m][];
       
      for (int i = 0; i < theArray.Length; i++)
         theArray[i] = new byte[n];
      

      You access the byte at x, y thus: theArray[x][y] Saving is a lot more efficient using a jagged array:

      //  wr is our BinaryWriter
      wr.Write(theArray.Length);  //  m
      wr.Write(theArray[0].Length);  //  n
        
      for (int i = 0; i < theArray.Length; i++)
         wr.Write(theArray[i]);
      

      Reading back in is also more efficient:

      //  rdr is a BinaryReader
      int m = rdr.ReadInt32();
      int n = rdr.ReadInt32();
      byte[][] theArray = new byte[m][];
       
      for (int i = 0; i < theArray.Length; i++)
         theArray[i] = rdr.ReadBytes(n);
      

      The jagged array code will also save non-rectangular arrays without modification.

      Ian Mariano - http://www.ian-space.com/
      "We are all wave equations in the information matrix of the universe" - me

      M 1 Reply Last reply
      0
      • I ian mariano

        Suppose wr is your BinaryWriter, your multidimensional (rectangular) array, theArray is 2D. Store and write the dimensions out thus (C#):

        int mupper = theArray.GetUpperBound(0);
        int nupper = theArray.GetUpperBound(1);
         
        ws.Write(mupper);
        ws.Write(nupper);
        

        Then write out the bytes:

        for (int i = 0; i < mupper; i++)
        {
           for (int j = 0; j < nupper; i++)
              wr.Write(theArray[i, j]);
        }
        

        To read it back in, you do the reverse:

        //  rdr is a BinaryReader
        int mupper = rdr.ReadInt32();
        int nupper = rdr.ReadInt32();
        byte[,] theArray = new byte[mupper, nupper];
         
        for (int i = 0; i < mupper; i++)
        {
           for (int j = 0; j < nupper; i++)
              theArray[i, j] = rdr.ReadByte();
        }
        

        The problem is that this is not that efficient. If, however, you used a jagged array (actually, the secondary array never differs in length so for all purposes it's still rectangular not jagged,) you can define theArray 2d, m,n like this:

        byte[][]  theArray = new byte[m][];
         
        for (int i = 0; i < theArray.Length; i++)
           theArray[i] = new byte[n];
        

        You access the byte at x, y thus: theArray[x][y] Saving is a lot more efficient using a jagged array:

        //  wr is our BinaryWriter
        wr.Write(theArray.Length);  //  m
        wr.Write(theArray[0].Length);  //  n
          
        for (int i = 0; i < theArray.Length; i++)
           wr.Write(theArray[i]);
        

        Reading back in is also more efficient:

        //  rdr is a BinaryReader
        int m = rdr.ReadInt32();
        int n = rdr.ReadInt32();
        byte[][] theArray = new byte[m][];
         
        for (int i = 0; i < theArray.Length; i++)
           theArray[i] = rdr.ReadBytes(n);
        

        The jagged array code will also save non-rectangular arrays without modification.

        Ian Mariano - http://www.ian-space.com/
        "We are all wave equations in the information matrix of the universe" - me

        M Offline
        M Offline
        Marco M
        wrote on last edited by
        #3

        the problem is the array is not an array of bytes.. it's an array of type double, declared this way: " double arr[,] x; " and the method "Write()" takes array of bytes as an argument... thanx for replying shakoosh

        I 1 Reply Last reply
        0
        • M Marco M

          the problem is the array is not an array of bytes.. it's an array of type double, declared this way: " double arr[,] x; " and the method "Write()" takes array of bytes as an argument... thanx for replying shakoosh

          I Offline
          I Offline
          ian mariano
          wrote on last edited by
          #4

          Using a jagged array instead of a rectangular one as above will do the trick. You just use double[][] x instead of double[,] x. See above. The code that would change would be the array initializers and accessors. If this is too much retrofit for the code, use the rectangular dual for loop method above.

          Ian Mariano - http://www.ian-space.com/
          "We are all wave equations in the information matrix of the universe" - me

          1 Reply Last reply
          0
          • M Marco M

            i tried to use FileStream Class but it only supports writing byte[] not even byte[,].. anyone has a clue ? thanx shakooosh

            R Offline
            R Offline
            Roman Rodov
            wrote on last edited by
            #5

            you need to devise your own save/load algorithm for example for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ ) for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) myFileStream.WriteByte(byte.Parse(myArray.GetValue( i, j ).ToString()) ); don't take my word on the syntax .... but you get the idea

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups