垂直枚举分组列
如果我有一个水平迭代然后垂直迭代的矩阵,它将像这样枚举:
0 1 2 3 4 5 6 7 8 9
------------------------------
0 | 1 2 3 4 5 6 7 8 9 10
1 | 11 12 13 14 15 16 17 18 19 20
2 | 21 22 23 24 25 26 27 28 29 30
如果我想垂直枚举我可以这样做:
total_rows * coln+ rown+ 1
0 1 2 3 4 5 6 7 8 9
--------------------------
0 |1 4 7 10 13 16 19 22 25 28
1 |2 5 8 11 14 17 20 23 26 29
2 |3 6 9 12 15 18 21 24 27 30
有人有方便的算法来垂直枚举分组列吗?
????
0 1 2 3 4 5 6 7 8 9
------------------------------
0 |1 2 7 8 13 14 19 20 25 26
1 |3 4 9 10 15 16 21 22 27 28
2 |5 6 11 12 17 18 23 24 29 30
If I have a matrix that is iterated horizontally and then vertically it would enumerate like this:
0 1 2 3 4 5 6 7 8 9
------------------------------
0 | 1 2 3 4 5 6 7 8 9 10
1 | 11 12 13 14 15 16 17 18 19 20
2 | 21 22 23 24 25 26 27 28 29 30
if I want to enumerate vertically I could do this:
total_rows * coln+ rown+ 1
0 1 2 3 4 5 6 7 8 9
--------------------------
0 |1 4 7 10 13 16 19 22 25 28
1 |2 5 8 11 14 17 20 23 26 29
2 |3 6 9 12 15 18 21 24 27 30
anybody have the algorithm handy to enumerate grouped columns vertically?
????
0 1 2 3 4 5 6 7 8 9
------------------------------
0 |1 2 7 8 13 14 19 20 25 26
1 |3 4 9 10 15 16 21 22 27 28
2 |5 6 11 12 17 18 23 24 29 30
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
即(组的总规模)*(您所在的组)
+(组中的水平位置)+(组的宽度)*(组中的垂直位置)+1
i.e. (total size of group)*(which group you're in)
+ (horizontal position in group) + (width of group) * (vertical position in group) +1
也许是这样的?
想想就很有趣^_^
Something like this perhaps?
This was fun to think about ^_^
通常,您使用嵌套循环迭代矩阵
,如果交换索引,这将枚举行:
那么您将按列枚举。
在您的情况下,您似乎有一个存储为普通数组的矩阵,因此您可以从作为寻址函数的两个循环中获取,正常的行访问是
(x/row_size)*row_size + x%row_size,因此您可以在切换到下一行之前迭代
row_size
元素。如果你稍微改变一下:
(x%col_size)*row_size + x/col_size
,你会得到一个函数,每次迭代都会添加row_size
(reching第n行),然后是一个值每个col_size
元素都会增加(因此每次完成一列时)。这应该有效..编辑:哦等等错过了分组因素,让我更新我的答案..你可以做类似的事情
Or in plain array style:
where
n
is the number of columns per groupUsually you iterate on a matrix with a nested loop
This will enumerate rows, if you swap indices:
Then you will enumerate by colums.
In your case you seem to have a matrix that is stored as a plain array so you can get from the two loops which is your addressing function, normal row access is
(x/row_size)*row_size + x%row_size
, so you iteraterow_size
elements before switching to the next row.If you change it slightly:
(x%col_size)*row_size + x/col_size
you obtain a function that ad every iteration addsrow_size
(reching nth row) and then a value that is increased everycol_size
elements (so every time you finish a column). This should work..EDIT: Oh wait missed that grouping factor, let me update my answer.. you can do something like
Or in plain array style:
where
n
is the number of columns per group