给出指数的三角矩阵的行和列

时间:2023-02-10 04:18:13

I am working with a MxM triangular matrix which has the following form:

我正在使用MxM三角矩阵,其形式如下:

M = [m00 m10 m20 m30 m40]
    [m11 m21 m31 m41    ] 
    [m22 m32 m42        ]
    [m33 m43            ]
    [m44                ]

If it's easier to picture this in terms of indexes, it would look like this:

如果根据索引更容易想象这一点,它看起来像这样:

M = [0 1 3 6 10]  
    [2 4 7 11  ]
    [5 8 12    ]
    [9 13      ]
    [14        ]

I know this way of indexing might look odd but it would be much easier if I could keep the indexing system as it is in order for this module to work well with others.

我知道这种索引方式可能看起来很奇怪,但是如果我能保持索引系统的原样,那么这个模块可以很好地与其他模块配合使用会更容易。

I am struggling with an algorithm that takes an index and size of the matrix that can return the row and column that the given index falls under. Ideally I would have 2 functions such as these:

我正在努力学习一种算法,该算法采用矩阵的索引和大小,该矩阵可以返回给定索引所属的行和列。理想情况下,我会有两个功能,如:

int getRow (int index, int size);
int getCol (int index, int size);

So getRow (7, 5) would return 3

因此getRow(7,5)将返回3

And getCol (7, 5) would return 1

并且getCol(7,5)将返回1

I have come across this thread already but I cannot seem to modify the solution given there to work for the way I am indexing.

我已经遇到过这个帖子,但我似乎无法修改那里的解决方案,因为我正在编写索引的方式。

algorithm for index numbers of triangular matrix coefficients

三角矩阵系数的索引数算法

1 个解决方案

#1


7  

New Answer

新答案

You can find the row and column using the following formulas:

您可以使用以下公式找到行和列:

int row = floor(-0.5 + sqrt(0.25 + 2 * index));
int triangularNumber = row * (row + 1) / 2;
int column = index - triangularNumber;

This works because the first item in each row is a triangular number (0, 1, 3, 6, 10, 15, ...). So the biggest triangular number that is lower than index gives us the row. Then column is simply the difference between index and that triangular number.

这是有效的,因为每行中的第一项是三角形数字(0,1,3,6,10,15,...)。因此,低于索引的最大三角形数字为我们提供了行。那么列就是索引和那个三角数之间的差异。

Also, note that you don't need the parameter M.

另请注意,您不需要参数M.


Old Answer

老答案

This code will give you both the row and column of index.

此代码将为您提供索引的行和列。

int triangularNumber = 0;
int row = 0;
while (triangularNumber + row < index) {
    row ++;
    triangularNumber += row;
}
int column = index - triangularNumber;

#1


7  

New Answer

新答案

You can find the row and column using the following formulas:

您可以使用以下公式找到行和列:

int row = floor(-0.5 + sqrt(0.25 + 2 * index));
int triangularNumber = row * (row + 1) / 2;
int column = index - triangularNumber;

This works because the first item in each row is a triangular number (0, 1, 3, 6, 10, 15, ...). So the biggest triangular number that is lower than index gives us the row. Then column is simply the difference between index and that triangular number.

这是有效的,因为每行中的第一项是三角形数字(0,1,3,6,10,15,...)。因此,低于索引的最大三角形数字为我们提供了行。那么列就是索引和那个三角数之间的差异。

Also, note that you don't need the parameter M.

另请注意,您不需要参数M.


Old Answer

老答案

This code will give you both the row and column of index.

此代码将为您提供索引的行和列。

int triangularNumber = 0;
int row = 0;
while (triangularNumber + row < index) {
    row ++;
    triangularNumber += row;
}
int column = index - triangularNumber;