-1

I want to transform a upper triangular matrix to a vector without the diagonal elements. I do notice the solution given in this question. Map upper triangular matrix on vector skipping the diagonal .

But my problem is little different. I want to transform a 5*5 matrix to vector like:

x; a12; a13; a14;  a15<br/>
x; x;   a23; a24; a25<br/>
x; x;  x;   a34;  a35<br/>
x;  x;  x;  x;  a45<br/

to

a12, a13, a14, a15, a23, a24, a25, a34, a35, a45

This means,<br/>
(i=1,j=2)->k=1; <br/>
(i=1,j=3)->k=2; <br/>
(i=1,j=4)->k=3; <br/>
(i=1,j=5)->k=4; <br/>
(i=2,j=3)->k=5; <br/>
(i=2,j=4)->k=6; <br/>
(i=2,j=5)->k=7; <br/>
(i=3,j=4)->k=8; <br/>
(i=3,j=5)->k=9; <br/>
(i=4,j=5)->k=10; <br/>

How can i get this relation f(i,j,N=5)=k? and how can i get the inverse function (i,j)=g(k,N) ?

Thank you!

Community
  • 1
  • 1
Z.Nico
  • 1
  • 3

1 Answers1

0

You have iterate the matrix by using an incrementing start index in a nested for-loop for each successive row while adding the elements to the vector.

for (rowI = 0; rowI < 4; rowI++) {
    for (colI = rowI+1; colI < 5; colI++) {
        vector.add(matrix[rowI][colI]);
    }
}
  • Outer loop iterates the rows.
  • Inner loop starts at index of outer loop +1 so that it tracks the inside edge of the upper triangular not including the diagonal.
  • Once you have the proper indices, just add it to the list.
  • The outer loop can stop one row short to save cycles because the inner loop is starting one index higher, so the stop condition will prevent it from doing anything anyway.

Alternatively if you just need the index, then a convenient one-liner is:
f(i,j,N) = (i-1)N + i(i-1)/2 + (j-i)

b4n4n4p4nd4
  • 70
  • 1
  • 10
  • Thank you. But what i want is the two relations – Z.Nico Oct 17 '16 at 14:14
  • f(i,j,N=5)=k and (i,j)=f(k,N) – Z.Nico Oct 17 '16 at 14:15
  • The nested structure above can answer the same question. Just replace the vector.add with k++ and start k at 0. Since the nested loop only iterates through valid positions in the upper triangular, the stopping point when rowI = i and colI = j will contain the proper value of K. Though since you don't need the actual values of the matrix in k, just the index of the position in the vector, it might be a more elegant solution to come up with a single expression that relates i, j, and N. I'm not exactly certain what is expected from the expression (i,j)=f(k,N). – b4n4n4p4nd4 Oct 17 '16 at 14:27
  • For the sake of a one-liner: f(i,j,N) = (i-1)N - (i+1)(i+2)/2 + (j-i). I haven't got an answer for the reverse direction yet. If you want the derivation, I can type it out. – b4n4n4p4nd4 Oct 18 '16 at 15:07