多维数组C# [英] multidimensional array c#

查看:92
本文介绍了多维数组C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的需要一个解决这个问题的想法.我想通过2个昏暗的数组,在此过程中,我需要减小它的尺寸.对于exp,我有一个[256,256]的多维数组,我需要将其减小为[64,64].为此,我需要计算4个元素的平方的平均值.对于exp [0,0] + [0,1] + [1,0] + [1,1]/4将是[64,64]数组中的第一个元素,[0,2] + [0, 3] + [1,2] + [1,3]/4将是第二个元素,依此类推.

我不想让您为我编写代码,我只想要一些想法或指示.

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

推荐答案

全面而全面的解决方案非常简单.您需要使用以下接口创建包装器类:

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

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
}



我介绍该接口仅是为了简化说明,并且因为实现不完整.您可以轻松完成它;您可以摆脱接口的限制,只需将其作为常规类成员即可实现.根据您的问题,我留给您.

你有照片吗?我已经完成了Implementation数组的维数,具体取决于构造函数的调用. SmallSquareSize属性定义虚拟尺寸.例如,如果Implementation为27x27,而SmallSquareSize为3,则虚拟尺寸为9x9,并且必须对数据进行平均3x3平方.因此,我为您的数据模型增加了一些灵活性.

例如,在无法将256除以3且无余数的情况下,请自行决定是否获得非整数的小方块.这不是一个大问题,因为您可以忽略其余区域,而仅提供大方块的平均值部分的数据.另外,如果基本尺寸不能除以小正方形的尺寸而没有余数,则可能引发异常,但我宁愿避免这种情况.

—SA



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


这篇关于多维数组C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆