Home > Archive > Matlab > October 2006 > Different size of matrix
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Different size of matrix
|
|
| Wenger 2006-10-31, 4:07 am |
| i'm trying to create different size of symmetric/sparse matrices. on
0 diagnol i want '2' and on -1 and 1 diagonal i want '-1', rest zeros
what commands do i need to use?
thanks
| |
| Dan Pearce 2006-10-31, 4:07 am |
| help diag
The example shows you what you need to do
| |
| Wenger 2006-10-31, 4:07 am |
| Dan Pearce wrote:
>
>
> help diag
>
> The example shows you what you need to do
i tried that, but i do not understand it and failing it. i need
matrices from size 2 to 20.
| |
| Wenger 2006-10-31, 4:07 am |
| thanks, i understand it now, just need to get on the 0 diagnol
| |
| Wenger 2006-10-31, 8:02 am |
| not really..
| |
| Wenger 2006-10-31, 8:02 am |
| diag(2:2)+diag(-3:-3, -1)+diag(-3:-3, 1)
that gives me 2by2 matrix: [2, -1; -1, 2]
how can i put this in for loop so i can get 3by3 matrix.... 10by10
matrix?
| |
| Dan Pearce 2006-10-31, 8:02 am |
| OK this is messy but it works and should give you enough to go on
size = 5;
diag((ones(2*size,1)*-1),1) + diag((ones(2*size,1)*-1),-1) +
diag(2*(ones(1,size*2+1)))
| |
| wenger 2006-10-31, 8:02 am |
| thanks, but i still need 2 on the zero diagonal.
when i add diag(2:2, 0) to that comman all zeros become 2 and -1
become 1
| |
| John D'Errico 2006-10-31, 8:02 am |
| wenger wrote:
>
>
> thanks, but i still need 2 on the zero diagonal.
>
> when i add diag(2:2, 0) to that comman all zeros become 2 and -1
> become 1
Various solutions use tools like
diag or spdiags.
Simpler yet is to use blktridiag.
<http://www.mathworks.com/matlabcent...objectType=FILE>
M = blktridiag(2,-1,-1,5);
It returns a sparse matrix, since the
result really is sparse. If you want
it as a full matrix, then just use
full.
full(blktridiag(2,-1,-1,5))
ans =
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
HTH,
John D'Errico
| |
| Randy Poe 2006-10-31, 8:02 am |
|
Wenger wrote:
> i'm trying to create different size of symmetric/sparse matrices. on
> 0 diagnol i want '2' and on -1 and 1 diagonal i want '-1', rest zeros
>
> what commands do i need to use?
A totally different approach from what has already been suggested:
Such a matrix is called "Toeplitz", and the TOEPLITZ function
can be used to generate it.
Since your matrix is symmetric, you can use the form where
you just specify the first row.
r = [2 -1 zeros(1,n-2)];
A = toeplitz(r);
- Randy
| |
| Titus Edelhofer 2006-10-31, 8:02 am |
| Hi,
besides the help from other posts:
help gallery
and look for the tridiag. If you want to go to 2D, take a look at
the poisson from gallery.
Titus
"Wenger" <akindian@sify.com> schrieb im Newsbeitrag
news:ef44e55.-1@webcrossing.raydaftYaTP...
> i'm trying to create different size of symmetric/sparse matrices. on
> 0 diagnol i want '2' and on -1 and 1 diagonal i want '-1', rest zeros
>
> what commands do i need to use?
>
> thanks
| |
| Wenger 2006-10-31, 8:02 am |
| thank you very much!!! :-D
|
|
|
|
|