Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
very specific symmetric matrix

Subject: very specific symmetric matrix

From: Eric

Date: 12 Jun, 2014 01:23:09

Message: 1 of 6

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)
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.

Subject: very specific symmetric matrix

From: Steven Lord

Date: 12 Jun, 2014 13:53:33

Message: 2 of 6


"Eric " <eric.malitz@gmail.com> wrote in message
news:lnavdt$e9i$1@newscl01ah.mathworks.com...
> 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
lines.

> 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
first
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
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: very specific symmetric matrix

From: Eric

Date: 13 Jun, 2014 04:58:13

Message: 3 of 6

"Steven Lord" <Steven_Lord@mathworks.com> wrote in message <lncbd8$rii$1@newscl01ah.mathworks.com>...
>
> "Eric " <eric.malitz@gmail.com> wrote in message
> news:lnavdt$e9i$1@newscl01ah.mathworks.com...
> > 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
> lines.
>
> > 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
> first
> 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
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Thanks; Is there some version of blkdiag or something else which allows you to put blocks on the off-diagonals, instead of only main diagonal?

Subject: very specific symmetric matrix

From: Bruno Luong

Date: 13 Jun, 2014 07:10:09

Message: 4 of 6


> Thanks; Is there some version of blkdiag or something else which allows you to put blocks on the off-diagonals, instead of only main diagonal?

spdiags(), it will creates sparse matrix, but can easity converted to regular matrix using full()

Bruno

Subject: very specific symmetric matrix

From: Eric

Date: 16 Jun, 2014 04:20:10

Message: 5 of 6

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <lne84h$19k$1@newscl01ah.mathworks.com>...
>
> > Thanks; Is there some version of blkdiag or something else which allows you to put blocks on the off-diagonals, instead of only main diagonal?
>
> spdiags(), it will creates sparse matrix, but can easity converted to regular matrix using full()
>
> Bruno

I don't see how that answers my question; I'm talking about taking blocks and placing them on diagonals; while there is a command to place them on the main diag (blkdiag), there doesn't seem to be a straightforward way to play blocks on the off-diagonals.

Subject: very specific symmetric matrix

From: Bruno Luong

Date: 16 Jun, 2014 07:31:08

Message: 6 of 6

"Eric" wrote in message <lnlr9p$f3u$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <lne84h$19k$1@newscl01ah.mathworks.com>...
> >
> > > Thanks; Is there some version of blkdiag or something else which allows you to put blocks on the off-diagonals, instead of only main diagonal?
> >
> > spdiags(), it will creates sparse matrix, but can easity converted to regular matrix using full()
> >
> > Bruno
>
> I don't see how that answers my question; I'm talking about taking blocks and placing them on diagonals; while there is a command to place them on the main diag (blkdiag), there doesn't seem to be a straightforward way to play blocks on the off-diagonals.

If your blocks is constant in size, might be you can you kron() combined with spdiags() to accomplish building matrix with the blocks off-diagonals

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us