Here you can find the source of vectorIndexToUpperTriangularIndices(int numberOfRows, int index)
Parameter | Description |
---|---|
numberOfRows | The number of rows of the matrix |
index | The index on the row-major vector |
public static int[] vectorIndexToUpperTriangularIndices(int numberOfRows, int index)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from ww w.j a v a2 s .co m*/ * @brief Transforms an index to its corresponding upper triangular indices * Let A = { * {11, 12, 13, 14}, * {21, 22, 23, 24}, * {31, 32, 33, 34}, * {41, 42, 43, 44} * } * be an array and b = {12, 13, 14, 23, 24, 34} be the row-major representation of the * upper triangular part of A, excluding the main diagonal. This method will translate * indices on vector b, to indices on matrix A. * * @param numberOfRows * The number of rows of the matrix * @param index * The index on the row-major vector * * @return The corresponding row and column indices on the matrix * * @sa upperTriangularIndicesToVectorIndex */ public static int[] vectorIndexToUpperTriangularIndices(int numberOfRows, int index) { int rowIndex = 0; int columnIndex; while (index >= numberOfRows - 1 - rowIndex) { index -= numberOfRows - 1 - rowIndex; rowIndex++; } columnIndex = rowIndex + 1 + index; return (new int[] { rowIndex, columnIndex }); } }