# Thread: Diagonal Matrix

1. ## Diagonal Matrix

I'm reading and trying to work through some problems in my book in preparation for up and coming Data Structures finals. I came across this question in my book and I'm sorta scratching my head on it.
Any dare to take a stab at it?

Mike
=======================
The main diagonal of a square matrix consists of the entries for which the row and column indices are equal. A diagonal matrix is a square matrix in which all entries not on the main diagonal are zero.

a) Describe a way to store a diagonal matrix without using space for entries that are zero.

b) Give the corresponding index function.

Here's what I deduce from my reading material.

a)
I’d implement a triangular table in a contiguous array by sliding each row out after the one above it. By placing each piece of data in the beginning of each row I store my data in a diagonal form.

b) (i,j) corresponds to 1/2 i (i+1)+j

----------------------------------------------------
Anyone care to sanity check me?

Mike

2. I do similar operations in matlab when dealing with matrices. The simplest way to store a diagonal matrix is to just store the diagonal in a single dimension array. so every none zero element will be Aii so you just store those elements in A[i].

and then if you have to you store the right hand side in another single dimension arraay so Rni is equal to R[i]

And I guess for the function you could do something like this

have them enter the index i and j
Code:
```if(i == j)
return A[i];
else if(i = n)   // n being the width of the matrix
return R[i];
else
return 0;```

Popular pages Recent additions