What is the best data structure for representing an upper triangular matrix in Java?

user3639557

Suppose an upper triangular matrix of integers is given. What's the best way of storing this in Java? The naive 2d int array is obviously not efficient. The solution I came up with was moved to the answers section.

user3639557

I think I found the solution. Here is my solution: Assume you have a 4X4 upper triangular matrix M.

1 2 3 4
0 6 7 1
0 0 8 9
0 0 0 5

If you can map every element of M in a 1d array, that's the best solution. All you need to know is to know which [row,col] of matrix corresponds to which element of your 1d array. Here is how you do the magic:

start_index=((col_index-1)+1)+((col_index-2)+1)+...+1
end_index=start_index + col_index

For example: if I want to find where are the elements on the 3rd column of the matrix, in the array:

start_index=((3-1)+1)+((3-2)+1)+((3-3)+1)=6
end_index=6+3=9

So, all I need to do is to start at index 6 of my array, and read all the elements till index 9 (including 9th element). Following this procedure, then you can store and retrieve all the cells of the nXn matrix in (n + n^2)/2 space.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Upper Triangular Matrix in Scala

From Dev

Upper Triangular Matrix in Scala

From Dev

Check Upper or Lower Triangular Matrix

From Dev

Linear index upper triangular matrix

From Dev

Create a horizontically stretched upper triangular matrix

From Dev

Melt the Upper Triangular Matrix of a Pandas Dataframe

From Dev

Scala fast generation of upper triangular matrix coordinates

From Dev

Map upper triangular matrix on vector skipping the diagonal

From Dev

How to create a specific upper triangular matrix?

From Dev

Outer function in R for upper triangular matrix

From Dev

How to create a specific upper triangular matrix?

From Dev

Reshape / Transform an upper triangular matrix in MATLAB

From Dev

What is the best way to create byte data structure not fixed size in JAVA

From Dev

What is the best data structure to implement a 2D Matrix with String data type?

From Dev

R: Convert upper triangular part of a matrix to symmetric matrix

From Dev

Convert a matrix in R into a upper triangular/lower triangular matrix with those corresponding entries

From Dev

Make a numpy upper triangular matrix padded with Nan instead of zero

From Dev

filling an upper triangular matrix (including diagonal) with a vector using RcppArmadillo

From Dev

Get upper triangular non-zero elements of a sparse matrix in R

From Dev

Finding upper/lower triangular form of arbitrary matrix n*n - python

From Dev

Fastest way to compute upper-triangular matrix of geometric series (Python)

From Dev

Get upper triangular non-zero elements of a sparse matrix in R

From Dev

Convert upper triangular part of a matrix to 3-column long format

From Dev

Best way of representing binary data

From Dev

Best way of representing binary data

From Java

Representing graphs (data structure) in Python

From Dev

What is the best data structure to store a hexagonal tilemap

From Dev

What is the best data structure to implement a queue?

From Dev

What Java data structure is best for two-way multi-value mapping

Related Related

  1. 1

    Upper Triangular Matrix in Scala

  2. 2

    Upper Triangular Matrix in Scala

  3. 3

    Check Upper or Lower Triangular Matrix

  4. 4

    Linear index upper triangular matrix

  5. 5

    Create a horizontically stretched upper triangular matrix

  6. 6

    Melt the Upper Triangular Matrix of a Pandas Dataframe

  7. 7

    Scala fast generation of upper triangular matrix coordinates

  8. 8

    Map upper triangular matrix on vector skipping the diagonal

  9. 9

    How to create a specific upper triangular matrix?

  10. 10

    Outer function in R for upper triangular matrix

  11. 11

    How to create a specific upper triangular matrix?

  12. 12

    Reshape / Transform an upper triangular matrix in MATLAB

  13. 13

    What is the best way to create byte data structure not fixed size in JAVA

  14. 14

    What is the best data structure to implement a 2D Matrix with String data type?

  15. 15

    R: Convert upper triangular part of a matrix to symmetric matrix

  16. 16

    Convert a matrix in R into a upper triangular/lower triangular matrix with those corresponding entries

  17. 17

    Make a numpy upper triangular matrix padded with Nan instead of zero

  18. 18

    filling an upper triangular matrix (including diagonal) with a vector using RcppArmadillo

  19. 19

    Get upper triangular non-zero elements of a sparse matrix in R

  20. 20

    Finding upper/lower triangular form of arbitrary matrix n*n - python

  21. 21

    Fastest way to compute upper-triangular matrix of geometric series (Python)

  22. 22

    Get upper triangular non-zero elements of a sparse matrix in R

  23. 23

    Convert upper triangular part of a matrix to 3-column long format

  24. 24

    Best way of representing binary data

  25. 25

    Best way of representing binary data

  26. 26

    Representing graphs (data structure) in Python

  27. 27

    What is the best data structure to store a hexagonal tilemap

  28. 28

    What is the best data structure to implement a queue?

  29. 29

    What Java data structure is best for two-way multi-value mapping

HotTag

Archive