垂直枚举分组列

发布于 2024-09-09 04:26:49 字数 688 浏览 10 评论 0原文

如果我有一个水平迭代然后垂直迭代的矩阵,它将像这样枚举:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

↘人皮目录ツ 2024-09-16 04:26:49
cols_per_group=2;

(total_rows*cols_per_group)*((int)(coln/cols_per_group))
+(coln%cols_per_group)+cols_per_group*rown +1

即(组的总规模)*(您所在的组)
+(组中的水平位置)+(组的宽度)*(组中的垂直位置)+1

cols_per_group=2;

(total_rows*cols_per_group)*((int)(coln/cols_per_group))
+(coln%cols_per_group)+cols_per_group*rown +1

i.e. (total size of group)*(which group you're in)
+ (horizontal position in group) + (width of group) * (vertical position in group) +1

所有深爱都是秘密 2024-09-16 04:26:49

也许是这样的?

for(group = 0; group < maxCol/2; group += 2)
{
    for(row = group; row < maxRows; row++)
    {
        for(col = 0; col < group + 2; col++)
        {
            matrix[col][row];
        }
    }
}

想想就很有趣^_^

Something like this perhaps?

for(group = 0; group < maxCol/2; group += 2)
{
    for(row = group; row < maxRows; row++)
    {
        for(col = 0; col < group + 2; col++)
        {
            matrix[col][row];
        }
    }
}

This was fun to think about ^_^

清音悠歌 2024-09-16 04:26:49

通常,您使用嵌套循环迭代矩阵

for (int i = 0; i < rows; ++i)
  for (int j = 0; j < cols; ++j)
    doSomething(matrix[i][j]);

,如果交换索引,这将枚举行:

for (int i = 0; i < rows; ++i)
  for (int j = 0; j < cols; ++j)
    doSomething(matrix[j][i]);

那么您将按列枚举。

在您的情况下,您似乎有一个存储为普通数组的矩阵,因此您可以从作为寻址函数的两个循环中获取,正常的行访问是 (x/row_size)*row_size + x%row_size,因此您可以在切换到下一行之前迭代 row_size 元素。

如果你稍微改变一下:(x%col_size)*row_size + x/col_size,你会得到一个函数,每次迭代都会添加row_size(reching第n行),然后是一个值每个 col_size 元素都会增加(因此每次完成一列时)。这应该有效..

编辑:哦等等错过了分组因素,让我更新我的答案..你可以做类似的事情

assert (cols % n == 0); /* we don't like not precise matrices */
for (int i = 0; i < cols / n; ++i)
  for (int j = 0; j < rows; ++j)
    for (int k = 0; k < n; ++n)
      doSomething(matrix[j][i+k]);

Or in plain array style:

(x%n) + row_size*(x/n) + (x / (col_size*n))*n
  ^          ^                  ^
  |          |                  |
  |          |               reposition after a group of columns
  |         moves vertically in the same group
 moves horizontally on the group

where n is the number of columns per group

Usually you iterate on a matrix with a nested loop

for (int i = 0; i < rows; ++i)
  for (int j = 0; j < cols; ++j)
    doSomething(matrix[i][j]);

This will enumerate rows, if you swap indices:

for (int i = 0; i < rows; ++i)
  for (int j = 0; j < cols; ++j)
    doSomething(matrix[j][i]);

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 iterate row_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 adds row_size (reching nth row) and then a value that is increased every col_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

assert (cols % n == 0); /* we don't like not precise matrices */
for (int i = 0; i < cols / n; ++i)
  for (int j = 0; j < rows; ++j)
    for (int k = 0; k < n; ++n)
      doSomething(matrix[j][i+k]);

Or in plain array style:

(x%n) + row_size*(x/n) + (x / (col_size*n))*n
  ^          ^                  ^
  |          |                  |
  |          |               reposition after a group of columns
  |         moves vertically in the same group
 moves horizontally on the group

where n is the number of columns per group

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文