How do I derive a function that would store non-zero elements of a lower triangu
ID: 3742475 • Letter: H
Question
How do I derive a function that would store non-zero elements of a lower triangular matrix into a sequence by row-order?
Let's say I have an n x n matrix where n = 5.
I already have the max number of non-zero elements, which is determined by the formula:
How do I figure out the formula, k, or f(i,j) where i represents the row and j represents the column, that would assign matrix element a[i,j] to its appropriate position in an array by order of row?
ex. a formula that would take a21 in the matrix above and generate the number 5, as it is the fifth matrix element by row-order.
EDIT: I am looking for a non-code expression for this formula, i.e. a mathematical formula similar to f(i,j) = (i-1)+3j that would find out the array position for each one of the matrix indices.
a1o 0 0 0 a22 0 0 10 - 20 21 22 a30 a31 332 333 40 41 42 43 44Explanation / Answer
you can get the appropriate position to store in array by formula
(i(i+1)/2)+j
if you want to store array from 0 index
or
(i(i+1)/2)+j+1
if you want to store array from 1 index
i consider storing from 0th index.. you can use either of formulae based on your
requirement...
where i is the row indices
and j is the column indices
example
you want to store a(0,0)
by above formula we get 0 so you store this in array 0 index
now a(2,1)
=(2(2+1)/2)+1
=4 you can store this element in array 4th index...
like wise.. if you consider storing from index 1.. formula 2 gives you 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.