• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Tridiagonal Matrix

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to construct a tridiagonal matrix.
The stipulation of this kind of matrix is the number of rows is equal to the number of columns and non-zero entries should only exist on the main diagonal and above and below the main diagonal e.g. this 6x6 array.
6 8 0 0 0 0
2 6 3 0 0 0
0 3 6 3 0 0
0 0 9 6 1 0
0 0 0 5 6 5
0 0 0 0 1 6

The main diagonal elements of this matrix are 6, 6, 6, 6, 6, 6.
How can I implement a program that obeys these rules when for example I want a user to enter the the size of the matrix and values in this matrix?
Thanks for the help
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
some thoughts for doing it quick and dirty...
i'd get the size, then create a 2-d array.
assuming you enter the data for the positions left to right, top to bottom,
you just read data for the array. the first row will have 2 (in [row,row] and [row,row+1]) elements (unless the array is 1x1), the last row will have 2 elements (in [row,row-1] and [row,row]), and every other row has 3 ([row,row-1], [row,row], [row,row+1]
so something like:
get size;
for (int row = 0; row < size; row++)
if row == 0
get one element [row,row]
if size > 1
get second element [row, row+1]
else if row == size - 1 //last row only has two datums in it
get 2 elements
else
get 3 elements
this might help. i'm sure there are better ways, but this might get you going....
reply
    Bookmark Topic Watch Topic
  • New Topic