开发者

Transform an upper triangular matrix into a full matrix C++

How would look the code that can transform an upper triangular matrix into a full matrix. The matrix is in a vector, not in a bidimensional array...

so the array

[ 1 2 3 4
  0 5 6 7
  0 0 8 9
  0 0 0 10 ]

would become an array like:

[ 1 2 3 4
  2 5 6 7
  3 6 8 9
  4 7 9 10 ]

could you provide som开发者_开发知识库e ideas, I was thinking in applying a kind of module or something...

There is one restriction, I am not using bidimensional arrays I am usng a vector, so is a unidimensional array


First, you must understand the fundemtnal nature of a reflected matrix. For any i, j, the following assertion is true:

m[i][j] ≡ m[j][i]

So, you need some algorithm to make that true. May I suggest:

for(int i = 0; i < HEIGHT; ++i)
  for(int j = 0; j < i; ++j)
    m[i][j] = m[j][i];

Note the condition of the 2nd loop. By ensuring that j is always less than i, we restrict our activity to the bottom-left triangle.

Next, you must understand how you have implemented a two-dimensional matrix in a one-dimensional array. It appears that you have established the identity:

m[i][j] ≡ v[i*WIDTH+j]

Substituting, we have:

for(int i = 0; i < HEIGHT; ++i)
  for(int j = 0; j < i; ++j)
    v[i*WIDTH+j] = v[j*WIDTH+i];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜