2

My question is how can I create the same type of matrix in a matlab program such that it keeps the same logic, that is 10s in the main diagonal, then 3s in the upper and lower diagonal surrounding the main diagonal, and ones in the diagonals above and below the 3s and 0s in other cases, and that I can be able to modify for whatever NxN I'd like?

something like this for a 6x6 case

A = [10  3  1  0  0  0 ; 
      3 10  3  1  0  0 ;
      1  3 10  3  1  0 ; 
      0  1  3 10  3  1 ; 
      0  0  1  3 10  3 ;
      0  0  0  1  3 10 ];
Shai
  • 111,146
  • 38
  • 238
  • 371
mpalmero
  • 23
  • 2
  • 1
    Did you try out anything? Maybe have a look here: http://stackoverflow.com/questions/3963565/how-to-assign-values-on-the-diagonal – gzm0 Apr 02 '13 at 23:48

2 Answers2

3

Code:

toeplitz([10  3  1  0  0  0])

Output:

ans =

    10     3     1     0     0     0
     3    10     3     1     0     0
     1     3    10     3     1     0
     0     1     3    10     3     1
     0     0     1     3    10     3
     0     0     0     1     3    10
JustinBlaber
  • 4,629
  • 2
  • 36
  • 57
  • Great thanks, but what if want to make it first a 50x50 and the a 1000x1000 and maybe then a 10000x10000 – mpalmero Apr 03 '13 at 02:12
  • 2
    @mpalmero To be honest you shouldn't even be forming the toeplitz explicitly since it's a waste of memory; it will also be a waste of flops to do arithmetic directly with the matrix as well. You can store the example matrix above with just 3 elements. But, if you want to reform it explicitly the easiest way would be to just reuse the `toeplitz` function and overwrite `A` again. Also don't forget to accept my response if it answers your question. – JustinBlaber Apr 03 '13 at 02:30
3

For very large matrices (N=10000) you'll have to use sparse matrix.
Consider the following construction using spdiags

function A = largeSparseMatrix( N )
%
% construct NxN sparse matrix with 10 on main diagonal, 
% 3 on the first off-diagonals and 1 on the second off-diagonals
%
B = ones(N, 5); % pre-allocate for diagonals
B(:,1) = 10;  % main diagonal d(1) = 0
B(:,2:3) = 3; % first off diagonal
B(:,4:5) = 1; % second off-diagonal
d = [ 0 , 1, -1, 2, -2 ]; % mapping columns of B to diagonals of A
A = spdiags( B, d, N, N ); % TA-DA!

Note that some of the entries in B are ignored in the construction of A.
See the manual of spdiags for more info.

Shai
  • 111,146
  • 38
  • 238
  • 371
  • When I execute the program and increase the N to let say 20 I cant't get a 20x20 matrix with 10 on main diagonal, 3 on the first off-diagonals and 1 on the second off-diagonals, what I'm doing wrong? – mpalmero Apr 04 '13 at 00:39
  • I double checked the code and it seems that is not generating the matrix I want – mpalmero Apr 04 '13 at 03:37
  • @mpalmero - my bad. I missed two arguments for `spdiags`. I edited my answer, please try it again. – Shai Apr 04 '13 at 05:12
  • Dear Shai, the output is still not accurate. What would be great for my question, would be that once I implement you code and excecute something like this largeSparseMatrix( 6 ), the output will be: A = [10 3 1 0 0 0 ; 3 10 3 1 0 0 ; 1 3 10 3 1 0 ; 0 1 3 10 3 1 ; 0 0 1 3 10 3 ; 0 0 0 1 3 10 ]; And if I increase the N re output would be whatever NxN matrix with the same structure. Thank you very much for all your support on this. – mpalmero Apr 04 '13 at 18:28
  • @mpalmero - in what sense the output now (after the fix) is different than you expect? are you familiar with the difference between full and sparse matrices? – Shai Apr 04 '13 at 22:22
  • Shai my bad, I forgot to apply the A=full(ans) command to convert the matrix into the regular form, your code is perfect – mpalmero Apr 05 '13 at 00:04
  • For very large matrices you might be better off if you leave the matrix in its sparse form. @mpalmero – Shai Apr 05 '13 at 08:30