Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I really need an ideea with this problem. I want to go through a 2 dim array and in the process i need to reduce the dimension of it. For exp I have an multidim Array of [256,256] and I need to reduce it to [64,64]. For this I need to calculate the average of squares of 4 elements. For exp [0,0]+[0,1]+[1,0]+[1,1]/4 will be the first element in the [64,64] array, [0,2]+[0,3]+[1,2]+[1,3]/4 will be the second element and so on.

I don`t want you to write the code for me , I just want some ideas or some directions
Posted
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 19:30pm    
Hey, this is nice and simple problem, but this is a good topic to be used for explanation of some elegant programming techniques. (I hope my solution is elegant enough :-)
Thanks for a good clearly formulated question. I voted 5, which happens very, very rarely with me here on CodeProject's Quick Questions & Answers.
--SA
Radu_Socaciu 29-Nov-11 19:36pm    
Thank you very much for your quick answer . This was very helpful for me .
I really apreciate it
Sergey Alexandrovich Kryukov 29-Nov-11 19:43pm    
You are very welcome. If you have any follow-up questions, there are welcome, too.
Good luck, call again.
--SA

1 solution

The total and comprehensive solution is fairly simple. You need to create a wrapper class with the following interface:

C#
class IArrayWrapper {
   ushort SmallSquareSize { get; set; }
   int CurrentDimension { get; }
   double this[int xIndex, int yIndex] { get; }
}

class ArrayWrapper : IArrayWrapper {
   internal ArrayWrapper(uint baseDimension) {
       Implementation = new double[baseDimension, baseDimension];
   }
   ushort IArrayWrapper.SmallSquareSize {
       get { return fSmallSquareSize; }
       set {
          if (fSmallSquareSize == value) return;
          fSmallSquareSize = value;
          //recalculate
       }
   }
   int IArrayWrapper.CurrentDimension { get { /* different dimensions, depending on SmallSquareSize */ } }
   double IArrayWrapper.this[int xIndex, int yIndex] { get { /* averaging or not... */ } }
   double[,] Implementation;
   ushort fSmallSquareSize = 1; //no averaging at first
}


I introduced the interface only for simplicity of explanation and because implementation is incomplete. You can easily complete it; and you can get rid of interface, implement it all as just regular class members. According to your question, I leave it to you.

Are you getting the picture? I've done your Implementation array dimensions depending on your constructor call. The SmallSquareSize property defines the virtual dimension. For example, if the Implementation is 27x27, and SmallSquareSize is 3, virtual dimension is 9x9 and you have to average data by the squared 3x3. So, I've added some flexibility to your data model.

Please decide by yourself if you get non-integer number of small squares when, for example, you cannot divide 256 by 3 without remainder. This is not a big problem as you can ignore remaining areas and provide data only for averages part of the big square. Alternatively, you could throw exception if the base dimension cannot be divided by the dimension of a small square without remainder, but I would rather avoid it.

—SA
 
Share this answer
 
v3
Comments
LanFanNinja 29-Nov-11 20:53pm    
+5 Very elegant!
Sergey Alexandrovich Kryukov 29-Nov-11 21:08pm    
Thank you very much.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900