For Programmers: Free Programming Magazines  


Home > Archive > Matlab > December 2006 > Checkerboard 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 Checkerboard Matrix
Louis Ehwerhemuepha

2006-12-27, 4:19 am

I am a new user of MATLAB (started on 23rd dec 06). I encountered a
problem in a tutorial by Edward Neuman of SIU. I am supposed to write
a program to produce a checkerboard matrix and this was the code I
could came up with. where n is odd.

function A = mysparse(n)

(% BAD CODE
% Produces a checkerboard matrix of the order n by n. Where n must be
odd.

C1 = 1;
C2 = [1 2; 2 3];
for K = 3:2:n
CK = C2 + 1;
end

A = blkdiag(C1, C2, CK);)

the code is only correct for n=3, for other values of n it breaks
down. Can someone help me with what is wrong?
Thanks.

The actual question is below:

A checkerboard matrix is a square block diagonal matrix, i.e., the
only nonzero entries are in
the square blocks along the main diagonal. In this exercise you are
to write MATLAB
function A = mysparse(n) that takes an odd number n and returns a
checkerboard matrix
as shown below
A = mysparse(3)
A =
1 0 0
0 1 2
0 3 4
A = mysparse(5)
A =
1 0 0 0 0
0 1 2 0 0
0 3 4 0 0
0 0 0 2 3
0 0 0 4 5
A = mysparse(7)
A =
1 0 0 0 0 0 0
0 1 2 0 0 0 0
0 3 4 0 0 0 0
0 0 0 2 3 0 0
0 0 0 4 5 0 0
0 0 0 0 0 3 4
0 0 0 0 0 5 6
First block in the upper-left corner is the 1-by-1 matrix while the
remaining blocks are all
2-by-2.
Roger Stafford

2006-12-27, 7:09 pm

In article <ef49821.-1@webcrossing.raydaftYaTP>, "Louis Ehwerhemuepha"
<delouisiano4peace@yahoo.com> wrote:

> I am a new user of MATLAB (started on 23rd dec 06). I encountered a
> problem in a tutorial by Edward Neuman of SIU. I am supposed to write
> a program to produce a checkerboard matrix and this was the code I
> could came up with. where n is odd.
>
> function A = mysparse(n)
>
> (% BAD CODE
> % Produces a checkerboard matrix of the order n by n. Where n must be
> odd.
>
> C1 = 1;
> C2 = [1 2; 2 3];
> for K = 3:2:n
> CK = C2 + 1;
> end
>
> A = blkdiag(C1, C2, CK);)
>
> the code is only correct for n=3, for other values of n it breaks
> down. Can someone help me with what is wrong?
> Thanks.
>
> The actual question is below:
>
> A checkerboard matrix is a square block diagonal matrix, i.e., the
> only nonzero entries are in
> the square blocks along the main diagonal. In this exercise you are
> to write MATLAB
> function A = mysparse(n) that takes an odd number n and returns a
> checkerboard matrix
> as shown below
> A = mysparse(3)
> A =
> 1 0 0
> 0 1 2
> 0 3 4
> A = mysparse(5)
> A =
> 1 0 0 0 0
> 0 1 2 0 0
> 0 3 4 0 0
> 0 0 0 2 3
> 0 0 0 4 5
> A = mysparse(7)
> A =
> 1 0 0 0 0 0 0
> 0 1 2 0 0 0 0
> 0 3 4 0 0 0 0
> 0 0 0 2 3 0 0
> 0 0 0 4 5 0 0
> 0 0 0 0 0 3 4
> 0 0 0 0 0 5 6
> First block in the upper-left corner is the 1-by-1 matrix while the
> remaining blocks are all
> 2-by-2.

-----------------------
If you like matlab's 'floor' function, here's a way to make good use of
it in your problem. Given that n is an odd positive integer, do this:

k = 1:(2*n-1);
r = k-1-3*floor((k-2)/4); r(1) = 1;
A = sparse(floor(k/2+1),k-2*floor(k/4),r);

I have assumed from your terminology that you wanted a sparse matrix result.

This isn't what I would have charcterized as a "Checkerboard Matrix", however.

Roger Stafford
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com