The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: very specific symmetric matrix
Replies: 5   Last Post: Jun 16, 2014 3:31 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 2,237
Registered: 9/26/13
Re: very specific symmetric matrix
Posted: Jun 12, 2014 9:53 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Eric " <> wrote in message
> Bear with me as I try to explain what I want. I doubt anyone here will be
> able to walk me through this, but I've tried many different things and I'm
> all out of options.
> In general, what I'm trying to get is a specific symmetric matrix where
> the entries are functions I have, evaluated at a grid of points (using the
> i and j corresponding to those grid points). What I first imagined doing
> was to try to make a matrix of function handles. Can't do this. The matrix
> I want is composed of (M-2)x(M-2) blocks of (N-2)x(N-2) entries. I have
> functions A, B, D and E. They are functions of 2 variables i,j (these come
> from the grid; the functions look like u(i+1,j)-2*u(i,j)+... etc). I want
> something resembling this:
> E(2,2) D(2,2) 0... B(2,2) A(2,2)
> 0.... 0.....
> D(2,3) E(2,3) D(2,3) 0... A(2,3) B(2,3) A(2,3)
> 0..... 0.....
> 0... D(2,4) E(2,4) D(2,4) 0.... A(2,4)
> B(2,4) A(2,4) 0.....
> 0 0 D(2,5) E(2,5)
> And this continues on. The main diagonal of the whole MATRIX is a block
> with E running down the main diagonal of this BLOCK but evaluated at
> (2,2), (2,3), (2,4), etc..
> The 2 off diagonals, next to E, are D, again evaluated at (2,2), (2,3),
> etc. as you go down in rows. The blocks to the right and under this block
> is the same idea but it's B along main diagonal and A on the 2 off
> diagonals. These blocks all continue down in this symmetric fashion, and
> the rest of is 0's. Again, (M-2)x(M-2) blocks of (N-2)*(N-2) entries. Let
> me do a specific example where N=M=5:
> E D 0 B A 0 0 0 0 <--- evaluated at (2,2)
> D E D A B A 0 0 0 <--evaluated at (2,3)
> 0 D E 0 A B 0 0 0 <--evaluated at (2,4)

This line is inconsistent with what you described above. The second 0 should
be D(2, 4), shouldn't it? There are similar inconsistencies with other

> B A 0 E D 0 B A 0<-- evaluated at (3,2)
> A B A D E D A B A<--evaluated at (3,3)
> 0 A B 0 D E 0 A B<-- evaluated at (3,4)
> 0 0 0 B A 0 E D 0<--evaluated at (4,2)
> 0 0 0 A B A D E D<--evaluated at (4,3)
> 0 0 0 0 A B 0 D E<-- evaluated at (4,4)
> I challenge anyone to help me, after hours and hours of getting nowhere
> with this.

If your functions are vectorized (can be evaluated for vectors of values,
not just scalars) then try the following:

% Define some coordinates
[c1, c2] = meshgrid(2:4);

% Define a function
E = @(x1, x2) x1+x2;

% Evaluate E to get a vector of data
v1 = E(c1(:), c2(:));

% Use v1 to create a matrix with E on the main diagonal and one of the
diagonals above the main.
% Since the diagonal above the main is shorter, I needed to cut a few
elements off the end of v1
% to make the matrix returned by the second DIAG call the same size as the
M = diag(v1, 0)+diag(v1(1:end-2), 2);

What you should receive is a 9-by-9 M matrix whose main diagonal is [4 5 6 5
6 7 6 7 8] and where each of those diagonal elements is duplicated two
elements to the right in their row.

This generalizes to multiple functions (not just the one E that I used) and
different diagonals.

If the E, D, etc. return values are matrices instead of vectors the same
size as the inputs, take a look at the BLKDIAG function, regular
concatenation, or (depending on the specific pattern) the CIRCSHIFT function

E1 = [1 2;3 4];
D1 = [5 6; 7 8];
z = zeros(2);
M2 = [E1 D1 z; D1 z E1; z E1 D1]

Steve Lord
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.