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. C#
  4. Referring to a 2D array through a 1D array

Referring to a 2D array through a 1D array

Scheduled Pinned Locked Moved C#
data-structuresquestion
12 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.
  • C crushinghellhammer

    This is what I would like to do: 1. Read in a WAV file 2. Break it up into segments of 256 samples each, with an overlap of 64 samples. 3. Refer to each segment as Segment[i] where "i" is the number of samples. I'm done with steps 1 and 2. This is the code block that I used to segment the samples: // *** CODE FOLLOWS *** // For the WAV file I'm working with, there are 17 such segments for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegment[i,j] = objWaveToSegment.Data[(i*64) + j]; } } /// *** END OF CODE *** In the above snippet, objWaveToSegment is an object of another class that reads in the WAV file and provides access to it's data. Now, how do I create 17 1*256 arrays called "Segment" such that when I refer to Segment[1] I get samples 0..255, Segment[2] is 64..319 etc. Thank you for your replies in advance :)

    A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #2

    Probably a too simple answer but I suppose this would do the job public WaveSegment[] GetWaveSegment(int index){ WaveSegment[] ws=new WaveSegment[256]; for (int arrIdx=0;arrIdx,256;arrIdx++0{ ws[arrIdx]=WaveSegment[index,arrIdx]; } return ws; } Maybe you could also investigate the Array.Copy static method

    C 1 Reply Last reply
    0
    • C crushinghellhammer

      This is what I would like to do: 1. Read in a WAV file 2. Break it up into segments of 256 samples each, with an overlap of 64 samples. 3. Refer to each segment as Segment[i] where "i" is the number of samples. I'm done with steps 1 and 2. This is the code block that I used to segment the samples: // *** CODE FOLLOWS *** // For the WAV file I'm working with, there are 17 such segments for(int i = 0; i < 17; i++) { for(int j = 0; j < 256; j++) { WaveSegment[i,j] = objWaveToSegment.Data[(i*64) + j]; } } /// *** END OF CODE *** In the above snippet, objWaveToSegment is an object of another class that reads in the WAV file and provides access to it's data. Now, how do I create 17 1*256 arrays called "Segment" such that when I refer to Segment[1] I get samples 0..255, Segment[2] is 64..319 etc. Thank you for your replies in advance :)

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #3

      Why not just use a jagged array, a.k.a. an array of arrays? When you get Segment[0] (note, all .NET languages use 0-based indexes, not 1-based indexes!) you get an array of 256 elements, whatever they are:

      WaveSegment[] segments = new WaveSegment[17];
      for (int i=0; iYou can then access them in several ways, including:

      WaveSegment[] innerSegments = segments[0];

      -----BEGIN GEEK CODE BLOCK-----
      Version: 3.21
      GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
      -----END GEEK CODE BLOCK-----

      C 1 Reply Last reply
      0
      • A Anonymous

        Probably a too simple answer but I suppose this would do the job public WaveSegment[] GetWaveSegment(int index){ WaveSegment[] ws=new WaveSegment[256]; for (int arrIdx=0;arrIdx,256;arrIdx++0{ ws[arrIdx]=WaveSegment[index,arrIdx]; } return ws; } Maybe you could also investigate the Array.Copy static method

        C Offline
        C Offline
        crushinghellhammer
        wrote on last edited by
        #4

        In this example, is WaveSegment[] a separate class that I perform the segmentation in? Or is it a function? I apologize for my ignorance, but I'm really new to all this.

        1 Reply Last reply
        0
        • H Heath Stewart

          Why not just use a jagged array, a.k.a. an array of arrays? When you get Segment[0] (note, all .NET languages use 0-based indexes, not 1-based indexes!) you get an array of 256 elements, whatever they are:

          WaveSegment[] segments = new WaveSegment[17];
          for (int i=0; iYou can then access them in several ways, including:

          WaveSegment[] innerSegments = segments[0];

          -----BEGIN GEEK CODE BLOCK-----
          Version: 3.21
          GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
          -----END GEEK CODE BLOCK-----

          C Offline
          C Offline
          crushinghellhammer
          wrote on last edited by
          #5

          Heath, thanks again for your help. Sorry for the index gaffe. I was aware of that, but was obviously not paying attention while typing. Could you please explain the following code: WaveSegment[] segments = new WaveSegment[17]; and WaveSegment[] innerSegments = segments[0]; What is WaveSegment[] here? A class, a function or a jagged array? I'm new to all this, and apologize for the dumb questions.

          H 1 Reply Last reply
          0
          • C crushinghellhammer

            Heath, thanks again for your help. Sorry for the index gaffe. I was aware of that, but was obviously not paying attention while typing. Could you please explain the following code: WaveSegment[] segments = new WaveSegment[17]; and WaveSegment[] innerSegments = segments[0]; What is WaveSegment[] here? A class, a function or a jagged array? I'm new to all this, and apologize for the dumb questions.

            H Offline
            H Offline
            Heath Stewart
            wrote on last edited by
            #6

            I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same. WaveSegment could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array of WaveSegment with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.

            -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

            C 3 Replies Last reply
            0
            • H Heath Stewart

              I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same. WaveSegment could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array of WaveSegment with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.

              -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

              C Offline
              C Offline
              crushinghellhammer
              wrote on last edited by
              #7

              Heath, thanks for your detailed replies! Ok, here's what I've done, incorporating your suggestions and that of my anonymous helper. public class Segmentation { private static void Main() { Random R = new Random(); int[] inArray = new int[1280]; for(int i = 0; i < 1280; i++) inArray[i] = i; int[][] arrSegmentArray = new int[17][]; for(int i = 0; i < 17; i++) { arrSegmentArray[i] = new int[256]; Array.Copy(inArray, 64*i, arrSegmentArray[i], 0, 256); Console.WriteLine(arrSegmentArray[i].ToString()); Console.ReadLine(); } } } However, where I expect arrSegmentArray[i] to be printed as a 256 element array, all I see is System.Int32[]; What do I need to fix here?

              1 Reply Last reply
              0
              • H Heath Stewart

                I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same. WaveSegment could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array of WaveSegment with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.

                -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                C Offline
                C Offline
                crushinghellhammer
                wrote on last edited by
                #8

                Heath, thanks for your detailed replies! Ok, here's what I've done, incorporating your suggestions and that of my anonymous helper. public class Segmentation { private static void Main() { Random R = new Random(); int[] inArray = new int[1280]; for(int i = 0; i < 1280; i++) inArray[i] = i; int[][] arrSegmentArray = new int[17][]; for(int i = 0; i < 17; i++) { arrSegmentArray[i] = new int[256]; Array.Copy(inArray, 64*i, arrSegmentArray[i], 0, 256); Console.WriteLine(arrSegmentArray[i].ToString()); Console.ReadLine(); } } } However, where I expect arrSegmentArray[i] to be printed as a 256 element array, all I see is System.Int32[]; What do I need to fix here? :(

                H 1 Reply Last reply
                0
                • H Heath Stewart

                  I don't know - you used it in your code fragment so I thought I'd reuse it. Maybe I'm using wrong in your context, but the idea is the same. WaveSegment could be anything: a class, struct, enum, or even a delegate! All you're doing is making an array of an array of something (no, I didn't stutter! :)). The first line made an array of WaveSegment with 17 elements. In my code fragment, I then created a new array of 256 elements within each of those 17 elements. The second line above merely gets that 256-element array from the first array element from the array with 17 elements. See Arrays[^] in the C# Language Features reference on MSDN, or more specifically Jagged Arrays[^] for more information.

                  -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                  C Offline
                  C Offline
                  crushinghellhammer
                  wrote on last edited by
                  #9

                  Solved it. Thanks for your help!

                  1 Reply Last reply
                  0
                  • C crushinghellhammer

                    Heath, thanks for your detailed replies! Ok, here's what I've done, incorporating your suggestions and that of my anonymous helper. public class Segmentation { private static void Main() { Random R = new Random(); int[] inArray = new int[1280]; for(int i = 0; i < 1280; i++) inArray[i] = i; int[][] arrSegmentArray = new int[17][]; for(int i = 0; i < 17; i++) { arrSegmentArray[i] = new int[256]; Array.Copy(inArray, 64*i, arrSegmentArray[i], 0, 256); Console.WriteLine(arrSegmentArray[i].ToString()); Console.ReadLine(); } } } However, where I expect arrSegmentArray[i] to be printed as a 256 element array, all I see is System.Int32[]; What do I need to fix here? :(

                    H Offline
                    H Offline
                    Heath Stewart
                    wrote on last edited by
                    #10

                    Glad you solved it, but just for future benefit, the line Console.WriteLine(arrSegmentArray[i].ToString()); is calling ToString on a Int32[] array. The default implementation of Array.ToString() (inherited from Object.ToString) is to simply print the namespace-qualified class name.

                    -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                    C 1 Reply Last reply
                    0
                    • H Heath Stewart

                      Glad you solved it, but just for future benefit, the line Console.WriteLine(arrSegmentArray[i].ToString()); is calling ToString on a Int32[] array. The default implementation of Array.ToString() (inherited from Object.ToString) is to simply print the namespace-qualified class name.

                      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                      C Offline
                      C Offline
                      crushinghellhammer
                      wrote on last edited by
                      #11

                      What I did was this: for (int i =0; i < 17; i++) { for (int j=0; j < 256; j++) { Console.Write(arrSegmentArray[i][j].ToString()+" "); } Console.WriteLine(); Console.ReadLine(); } Is that alright?

                      H 1 Reply Last reply
                      0
                      • C crushinghellhammer

                        What I did was this: for (int i =0; i < 17; i++) { for (int j=0; j < 256; j++) { Console.Write(arrSegmentArray[i][j].ToString()+" "); } Console.WriteLine(); Console.ReadLine(); } Is that alright?

                        H Offline
                        H Offline
                        Heath Stewart
                        wrote on last edited by
                        #12

                        Sure, why not?! :) It just depends on your requirements and implementation.

                        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

                        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