将二维上三角和下三角中的元素映射到线性结构 [英] Mapping elements in 2D upper triangle and lower triangle to linear structure

查看:22
本文介绍了将二维上三角和下三角中的元素映射到线性结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵 M,它有 NxN 维,其中 M(i,j) = M(j,i)

I have a matrix M which is of NxN dimensions, where M(i,j) = M(j,i)

我想将此结构表示为 (N²+N)/2 线性阵列 K,以节省空间.我的问题是提出将 M(min(i,j),min(i,j)) 映射到范围 [0,(N^2)/2)

I would like to represent this structure as a (N²+N)/2 linear array K, to save space. My problem is coming up with the formula that will map a M(min(i,j),min(i,j)) into a range [0,(N^2)/2)

下面是带有 K 线性阵列索引的 3x3 矩阵的映射,X 表示这些单元不存在,而是使用它们的转置:

Below is a mapping of a 3x3 matrix with indexes for K linear array, the X means those cells don't exist and instead their transpose is to be used:

0123
X456
XX78
XXX9

这是一个带有 K 线性数组索引的 7x7 矩阵

Here is a 7x7 matrix with indexes for the K linear array

     0  1  2  3  4  5  6
 0  00 01 02 03 04 05 06
 1     07 08 09 10 11 12
 2        13 14 15 16 17
 3           18 19 20 21
 4              22 23 24
 5                 25 26
 6                    27

目前我有以下内容

int main()
{
   const unsigned int N = 10;
   int M[N][N];

   int* M_ = &(M[0][0]);

   assert(M[i][j] = M_[N * min(i,j) + max(i,j)]);

   //int* K = .....
   //assert(M[i][j] = K[.....]);

   return 0;
}

推荐答案

我需要的相反方向:

void printxy(int index)  
{  
    int y = (int)((-1+sqrt(8*index+1))/2);  
    int x = index - y*(y+1)/2;  
}

这篇关于将二维上三角和下三角中的元素映射到线性结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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