一维数组二维数组映射 [英] 1D array to 2D array mapping

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

问题描述

这听起来像一个家庭作业的问题,但我发誓,事实并非如此。

This may sound like a homework problem, but I swear it isn't.

我试图建立一个迭代器为这个二维数组包装类。我想,如果我能解决这个问题,那么我就可以建立我的迭代器。

I'm trying to build an iterator for this 2D-array wrapper class. I figured that if I am able to solve this problem, then I can build my iterator.

我已经连续9整数此一维数组从0开始,并在8结束。

I have this 1D array of 9 consecutive integers starting at 0 and ending at 8.

[0,1,2,3,4,5,6,7,8]

我给两个变量 horizo​​ntal_size = 3 vertical_size = 3

我想使这个数组为二维数组,它是 horizo​​ntal_size vertical_size 。我们姑且称之为 ^ h v 为了简便起见。

I want to make this array into a 2D array that is horizontal_size by vertical_size. let's call them h and v for brevity.

这是我要生成的结果是这样的:

The result that I want to generate is this:

0 1 2
3 4 5
6 7 8

由于一维数组中的值,它告诉我的指标,还给出 ^ h v ,其中均3在这种情况下。有没有一种方法来生成二维数组的索引?

Given the value in the 1D array, which tells me the index, Also given h and v, which are both 3 in this case. Is there a way to generate the indices on the 2D array?

例如,一维数组中的第一个元素为0,它映射到数组[0] [0] 。第二个因素是1,它映射到数组[0] [1]

For example, the first element in the 1D array is 0, which maps to array[0][0]. The second element is 1, which maps to array[0][1]

我想通了,我可以做得到垂直指数 array1d [I] MOD vertical_size

I figured out that I can get the vertical index by doing array1d[i] mod vertical_size.

           for getting the vertical index ::: th 

0 = [0] [0] 0模3 = 0 1 = [0] [1] 1模3 = 1 2 = [0] [2] 2模3 = 2

0 = [0][0] 0 mod 3 = 0 1 = [0][1] 1 mod 3 = 1 2 = [0][2] 2 mod 3 = 2

3 = [1] [0] ...等 4 = [1] [1] 5 = [1] [2]

3 = [1][0] and so on... 4 = [1][1] 5 = [1][2]

6 = [2] [0] 7 = [2] [1] 8 = [2] [2]

6 = [2][0] 7 = [2][1] 8 = [2][2]

但我不知道怎么去水平指数。

But I'm not sure how to get the horizontal index.

推荐答案

水平指数由给地板(I / V),或只是 I / V 如果你的编程语言,通过截断实现整数除法。

The horizontal index is given by floor(i / v), or as just i/v if your programming language implements integer division by truncation.

例如,地板(7/3)= 2,所以图7是第2行的上

For example, floor(7/3) = 2, so 7 is on the row 2.

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

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