Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 转置一维数组_C++_C_Algorithm - Fatal编程技术网

C++ 转置一维数组

C++ 转置一维数组,c++,c,algorithm,C++,C,Algorithm,我有一个一维数组,有N个值,其中N是一个完美的正方形。我将这个一维数组可视化为二维数组(尽管它不是)。例如,值为int-array={0,1,2,3,4,5,6,7,8} 就是 int *Array = new int [9];

我有一个一维数组,有N个值,其中N是一个完美的正方形。我将这个一维数组可视化为二维数组(尽管它不是)。例如,值为
int-array={0,1,2,3,4,5,6,7,8}

就是

int *Array = new int [9];                                                                                                                                                                                                    
for ( int i = 0 ; i < 9 ; i ++ )
         Array[i] = i; // For example
所以,我想交换一维数组中的位置,这样我就能得到它的转置

例如

0 3 6
1 4 7
2 5 8
这基本上是同一个一维数组,但是值被交换,因此数组现在是
int-array={0,3,6,1,4,7,2,5,8}

如果我将它扩展到一个1024*1024维的数组,那么逻辑将是怎样的?

使用
n=sqrt(n)
,您可以尝试一些简单的方法,例如:

for(int i = 0; i < n; ++i)
    for(int j = i+1; j < n; ++j)
        std::swap(Array[n*i + j], Array[n*j + i]);
for(int i=0;i
转置操作执行
交换(v[y][x],v[x][y])
上下三角形,不包括矩阵的对角线(比如上三角形)

在C一维向量中,
v[y][x]
对应于
vc[y*n+x]
。 所以你想做
vc[y*n+x]=vc[x*n+y]

要交换的元素是
x>y
的元素

你最终会做:

for(int y = 0; y < n; ++y)
    for(int x = y+1; x < n; ++x)
        swap(vc[x*n + y], vc[y*n + x]);
for(int y=0;y

如果不使用交换功能,您可以自己解决这个问题…

。len是数组的长度

int i,j;
  N = sqrt(len);    
  int temp[len];
    for(i=0;i<N;i++)
    {   for(j=0;j<N;j++)
        {
            temp[j+(i*N)] = a[(j*N)+i];
        }
    }
inti,j;
N=sqrt(len);
内部温度[透镜];
对于(i=0;i
#包括
#包括
使用名称空间std;
int xyToIndex(常量int x、常量int y、常量int大小){
返回x+y*大小;
}
int main(){
int a[]={0,1,2,3,4,5,6,7,8};
常量int size=sqrt(sizeof(a)/sizeof(int));
//仅打印
用于(int x=0;xcout您可以交换矩阵中的值,也可以交换后面函数中的解释

例如,您可以打印a(j,i)并打印变换姿势,而不是打印a(i,j)

也就是说,你想做什么呢?如果你看看LAPACK和BLAS,它们的例程会使用控制算法的标志来正常解释它们或将它们转换

static unsigned other(unsigned dim0, unsigned dim1, unsigned index)
{

#if 0
unsigned x0,x1;
x0 = index % dim0 ;
x1 = index / dim0 ;

return x0 * dim1 + x1;

#else

unsigned mod,val;

mod = dim0 * dim1 -1;
val = (index==mod) ? mod: (dim1*index) % mod;
return val;
#endif
}
上述函数返回索引的“其他”索引(转置矩阵中的索引:=x和y交换)。 dim0和dim1是矩阵的“水平”和“垂直”大小。#ifdefed out部分是简单的实现。 在您的情况下,您可以使用以下方法初始化(或分析)一维数组:

for (i=0; i < 9; i++)
    arr[i] = other(3, 3, i);
(i=0;i<9;i++)的

arr[i]=其他(3,3,i);

哈哈。这是一个超级快速的答案;)你的数组是一维数组吗?是的。谢谢你的快速回答!;)
static unsigned other(unsigned dim0, unsigned dim1, unsigned index)
{

#if 0
unsigned x0,x1;
x0 = index % dim0 ;
x1 = index / dim0 ;

return x0 * dim1 + x1;

#else

unsigned mod,val;

mod = dim0 * dim1 -1;
val = (index==mod) ? mod: (dim1*index) % mod;
return val;
#endif
}
for (i=0; i < 9; i++)
    arr[i] = other(3, 3, i);