转置前导维度为 N 的一维数组

发布于 2024-08-27 03:15:57 字数 43 浏览 7 评论 0原文

如何在没有额外空间的情况下转置前导维度为 N 的一维数组?任何语言都可以

how can i transpose an 1d array of leading dimension N, without extra space ? any language is fine

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

不气馁 2024-09-03 03:15:57

我的一维就地矩阵转置解决方案

  mn    = M*N;      /* M rows and N columns */

  q     = mn - 1;

  i = 0;      /* Index of 1D array that represents the matrix */

  do {

    k = (i*M) % q;

    while (k>i) k = (M*k) % q;

    if (k!=i) Swap(k, i);

  } while ( ++i <= (mn -2) );

  /* Update row and column */

  matrix.M = N;

  matrix.N = M;

My solution for 1D in-place Matrix transposition

  mn    = M*N;      /* M rows and N columns */

  q     = mn - 1;

  i = 0;      /* Index of 1D array that represents the matrix */

  do {

    k = (i*M) % q;

    while (k>i) k = (M*k) % q;

    if (k!=i) Swap(k, i);

  } while ( ++i <= (mn -2) );

  /* Update row and column */

  matrix.M = N;

  matrix.N = M;
记忆里有你的影子 2024-09-03 03:15:57

将表示为线性数组的非方矩阵就地转置有点麻烦
的一个技巧。

这是一个 REXX 程序,它执行就地转置
用 1 表示的 2D 矩阵
大小为 M * N 的维数组,其中 M
行和
N 是其中的列数
非转置数组。转置后,M 变为列数,
N 成为行数。

i = 0                /* linear array index */ 
do j = 1 to N        /* j = 1 to columns of non-transposed array */ 
  do k = 1 to M      /* k = 1 to rows of non-transposed array */ 
    i = i + 1
    t = (k - 1) * N + j 
    do while t < i 
      jp = (t - 1) % M + 1 
      kp = t - (jp - 1) * M 
      t = (kp - 1) * N + jp 
    end 
    if i \= t then say 'exchange('i',' t')'   /* swap elements */
  end 
end

上面的程序显示了必须交换才能产生转置数组的数组元素。

该程序适用于任何
大小矩阵表示为元素排列的线性数组
按列然后按行。例如,M × N 矩阵的元素排列如下:

X1,1, X1,2, ... X1,N , X2,1, X2,2, ... X2,N, ... XM,N

程序打印出需要交换的元素的线性数组索引以产生
如下形式的转置矩阵:

X1,1, X2,1, ... XM,1, X 1,2, X2,2, ... XM,2, ... XM,N

对于例如,从 M = 4、N = 2 和一个包含以下内容的数组开始:

A1, B1, A2, B2, A3, B3, A4, B4

执行以下交换:

exchange(2, 3) 
exchange(3, 5) 
exchange(4, 7) 
exchange(6, 7)

排列变为:

A1、A2、A3、A4、B1、B2、B3、B4

这是如何工作的?

从一个符号开始,将线性数组中的每个元素标识为:

X[i,j,k] 

where: i is the linear array index for an element in X 
       j is the row number for element in X
       k is the column number for element in X

使用此符号,行主要顺序的数组可以生成为:

i = 0 
do j = 1 to M    /* Row counter */ 
  do k = 1 to N  /* Column counter */ 
    i = i + 1     /* array index of element j, k */ 
    say 'X['i','j','k']' 
  end 
end

请注意,给定 j(行)和 k(列)、i 的值, Xi,j 的线性数组索引可以计算为:

i = (j - 1) * N + k 

矩阵 X 的转置是通过交换元素构造的
X[i,j,k],其中 X[t,k,j]j = 1 到 M 范围内,并且 >k = 1 到 N 假设 i > t。在
本质我们交换一切
行变量用于列变量。使用上述符号,这相当于
交换线性数组元素:

exchange(X[i,j,k], X[t,k,j])

给定 jk 的值,我们可以计算 it 的值: :

   i = (j - 1) * N + k
   t = (k - 1) * M + i

现在,随着 i 从 1 增加到 M * N,继续进行线性数组进行上述交换。
请注意,在每次迭代之后,X 中索引小于或等于 i 的所有元素都具有
被转置了。这是
因为只有当正确的元素已经存在时,每次迭代才会完成
交换为X[i]

每当i>时t,这意味着先前的交换发生在索引t处。 t 处的元素是
如上所示进行交换,将其放置在某个新位置tprime。给定一个
索引t,我们可以计算行主索引tprime以及行和
与其关联的列号如下:

jprime = (t - 1) % M + 1
k素数 = t - (j素数 - 1) * M
tprime = (kprime - 1) * N + jprime

同样,如果tprime 小于i,表示该元素被交换
我们也必须继续
进行另一轮计算。将 t 设置为计算出的 tprime 并重复。最终,
变得小于t并且可以完成交换。基本上我们通过它的所有元素来跟踪该元素
之前的交换,直到我们在线性数组中的 ii 的右侧找到它。

对线性数组中的每个元素重复此循环,矩阵将被转置。

Transposing a non-square matrix in-place represented as a linear array is a bit
of a trick.

Here is a REXX program that performs an in-place transpose of
a 2D matrix represented in a one
dimensional array of size M * N where M is the number of
rows and
N is the of columns in the
non-transposed array. Once transposed, M becomes the number of columns and
N becomes the number of rows.

i = 0                /* linear array index */ 
do j = 1 to N        /* j = 1 to columns of non-transposed array */ 
  do k = 1 to M      /* k = 1 to rows of non-transposed array */ 
    i = i + 1
    t = (k - 1) * N + j 
    do while t < i 
      jp = (t - 1) % M + 1 
      kp = t - (jp - 1) * M 
      t = (kp - 1) * N + jp 
    end 
    if i \= t then say 'exchange('i',' t')'   /* swap elements */
  end 
end

The above program displays array elements that must be exchanged to yield a transposed array.

This program will work for any
sized matrix represented as a linear array where the elements are arranged
by column then row. For example, an M by N matrix will have its elements arranged as follows:

X1,1, X1,2, ... X1,N, X2,1, X2,2, ... X2,N, ... XM,N

The program prints out the linear array indices of the elements that need to be exchanged to yield
a transposed matrix of the form:

X1,1, X2,1, ... XM,1, X1,2, X2,2, ... XM,2, ... XM,N

For example, start with M = 4, N = 2 and an array containing:

A1, B1, A2, B2, A3, B3, A4, B4

Perform the following exchanges:

exchange(2, 3) 
exchange(3, 5) 
exchange(4, 7) 
exchange(6, 7)

and the arrangement becomes:

A1, A2, A3, A4, B1, B2, B3, B4

How does this work?

Start with a notation to identify each element in the linear array as:

X[i,j,k] 

where: i is the linear array index for an element in X 
       j is the row number for element in X
       k is the column number for element in X

Using this notation, an array in row major order can be generated as:

i = 0 
do j = 1 to M    /* Row counter */ 
  do k = 1 to N  /* Column counter */ 
    i = i + 1     /* array index of element j, k */ 
    say 'X['i','j','k']' 
  end 
end

Note that given values for j (row) and k (column), i, the linear array index for Xi,j, may be calculated as:

i = (j - 1) * N + k 

The transpose of matrix X is constructed by exchanging elements
X[i,j,k] with X[t,k,j] over the range of j = 1 to M and k = 1 to N provided i > t. In
essence we exchange all
row variables for column variables. Using the notation described above, this amounts
to exchanging linear array elements:

exchange(X[i,j,k], X[t,k,j])

Given values for j and k we can calculate the values of i and t as:

   i = (j - 1) * N + k
   t = (k - 1) * M + i

Now, proceed through the linear array making the above exchanges as i is increased from 1 to M * N.
Notice that after each iteration all elements of X having an index less than or equal to i have
been transposed. This is
because each iteration only completes when the correct element has been
exchanged into X[i].

Whenever i > t, it means that a prior exchange took place at index t. The element at t was
exchanged as indicated above placing it at some new position tprime. Given an
index t, we may calculate the row major index tprime as well as the row and
column numbers associated with it as follows:

jprime = (t - 1) % M + 1
kprime = t - (jprime - 1) * M
tprime = (kprime - 1) * N + jprime

Again, if tprime is less than i, it means that this element was exchanged
too and we must continue
with another round of calculations. Set t to the calculated tprime and repeat. Eventually, i will
become less than t and the exchange may be done. Basically we follow the element through all of its
prior exchanges until we find it at i or to the right of i in the linear array.

Repeat this cycle for each element in the linear array and the matrix will have been transposed.

给我一枪 2024-09-03 03:15:57

最简单的方法就是:

for each m
  for each n
    if m != n 
       swap A[m][n] and A[n][m]

当然,这仅适用于方阵。对于矩形矩阵的就地转置,事情变得有点棘手。

Simplest approach is just:

for each m
  for each n
    if m != n 
       swap A[m][n] and A[n][m]

This only works for square matrices of course. For in-place transpose of rectangular matrices things get a little trickier.

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