COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### creating diagonal matrix

• Email
• Follow

```Hi,
Let say I have a vector B=[1 0] and I want to use this vector B repeating in this matrix A having size [8x16].

First, I initialize the matrix A with all zeros then I want to have the outcome as below:
A=
[1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0;
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0];

How can I use that vector B repeat several times on matrix A using some code? It looks like an eye matrix but the pattern is little bit different. I tried to do some looping but still doesn't get it as the above.

Second question:
I still want to use vector B to create another matrix C with size [4x8] as below and how to call it without constructing it manually?
C=
[1 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0;
0 0 0 0 1 0 0 0;
0 0 0 0 0 1 0 0];

```
 0

See related articles to this posting

```For the first one, try this

B=[1 0];
A=blkdiag(B,B,B,B,B,B,B,B);

Mongkut
```
 0

```"Mongkut Piantanakulchai" <mongkutp.remove.this@gmail.com> wrote in message <hs45g9\$rok\$1@fred.mathworks.com>...
> For the first one, try this
>
> B=[1 0];
> A=blkdiag(B,B,B,B,B,B,B,B);
>
> Mongkut

Thanks Mongkut.It does help but I have to repeat the B for 8 times. Is there any other way for making it more flexible especially for larger size?

I have other solution for question 1 by creating eye matrix of M size and then I want to remove all the even row. I knew we can delete row by specifying all the number of rows but it is restricted to smaller size of matrix. Instead of repeating the number of rows index, I want to make it flexible for greater size.

```
 0

```"sarrah " <gto_girlz83@yahoo.com> wrote in message <hs63r8\$j9i\$1@fred.mathworks.com>...
> "Mongkut Piantanakulchai" <mongkutp.remove.this@gmail.com> wrote in message <hs45g9\$rok\$1@fred.mathworks.com>...
> > For the first one, try this
> >
> > B=[1 0];
> > A=blkdiag(B,B,B,B,B,B,B,B);
> >
> > Mongkut
>
> Thanks Mongkut.It does help but I have to repeat the B for 8 times. Is there any other way for making it more flexible especially for larger size?
>

A = repmat({B},1,888);
A = blkdiag(A{:});

Better yet is to make the result a sparse matrix.

A = repmat({sparse(B)},1,888);
A = blkdiag(A{:});

whos A
Name        Size              Bytes  Class     Attributes
A         888x1776            28424  double    sparse

John
```
 0

```"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hs65sr\$c9\$1@fred.mathworks.com>...
> "sarrah " <gto_girlz83@yahoo.com> wrote in message <hs63r8\$j9i\$1@fred.mathworks.com>...
> > "Mongkut Piantanakulchai" <mongkutp.remove.this@gmail.com> wrote in message <hs45g9\$rok\$1@fred.mathworks.com>...
> > > For the first one, try this
> > >
> > > B=[1 0];
> > > A=blkdiag(B,B,B,B,B,B,B,B);
> > >
> > > Mongkut
> >
> > Thanks Mongkut.It does help but I have to repeat the B for 8 times. Is there any other way for making it more flexible especially for larger size?
> >
>
> A = repmat({B},1,888);
> A = blkdiag(A{:});
>
> Better yet is to make the result a sparse matrix.
>
> A = repmat({sparse(B)},1,888);
> A = blkdiag(A{:});
>
> whos A
>   Name        Size              Bytes  Class     Attributes
>   A         888x1776            28424  double    sparse
>
> John
----------------------------------------
Thanks a lot John!!
How about I still want to use the same vector B[1 0] creates this D matrix as below:
D=
[1 0 0 0 0 0 0 0;
0 1 0 0 0 0 0 0;
0 0 0 0 1 0 0 0;
0 0 0 0 0 1 0 0];

I don't think we can use the blkdiag function again. Any idea?
```
 0

```"sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6764\$lvb\$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hs65sr\$c9\$1@fred.mathworks.com>...
> > "sarrah " <gto_girlz83@yahoo.com> wrote in message <hs63r8\$j9i\$1@fred.mathworks.com>...
> > > "Mongkut Piantanakulchai" <mongkutp.remove.this@gmail.com> wrote in message <hs45g9\$rok\$1@fred.mathworks.com>...
> > > > For the first one, try this
> > > >
> > > > B=[1 0];
> > > > A=blkdiag(B,B,B,B,B,B,B,B);
> > > >
> > > > Mongkut
> > >
> > > Thanks Mongkut.It does help but I have to repeat the B for 8 times. Is there any other way for making it more flexible especially for larger size?
> > >
> >
> > A = repmat({B},1,888);
> > A = blkdiag(A{:});
> >
> > Better yet is to make the result a sparse matrix.
> >
> > A = repmat({sparse(B)},1,888);
> > A = blkdiag(A{:});
> >
> > whos A
> >   Name        Size              Bytes  Class     Attributes
> >   A         888x1776            28424  double    sparse
> >
> > John
> ----------------------------------------
> Thanks a lot John!!
> How about I still want to use the same vector B[1 0] creates this D matrix as below:
> D=
> [1 0 0 0 0 0 0 0;
>  0 1 0 0 0 0 0 0;
>  0 0 0 0 1 0 0 0;
>  0 0 0 0 0 1 0 0];
>
> I don't think we can use the blkdiag function again. Any idea?

---Add information to the above question. The problem now is to remove the multiple row alternately
```
 0

```"sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6764\$lvb\$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hs65sr\$c9\$1@fred.mathworks.com>...
> > "sarrah " <gto_girlz83@yahoo.com> wrote in message <hs63r8\$j9i\$1@fred.mathworks.com>...
> > > "Mongkut Piantanakulchai" <mongkutp.remove.this@gmail.com> wrote in message <hs45g9\$rok\$1@fred.mathworks.com>...
> > > > For the first one, try this
> > > >
> > > > B=[1 0];
> > > > A=blkdiag(B,B,B,B,B,B,B,B);
> > > >
> > > > Mongkut
> > >
> > > Thanks Mongkut.It does help but I have to repeat the B for 8 times. Is there any other way for making it more flexible especially for larger size?
> > >
> >
> > A = repmat({B},1,888);
> > A = blkdiag(A{:});
> >
> > Better yet is to make the result a sparse matrix.
> >
> > A = repmat({sparse(B)},1,888);
> > A = blkdiag(A{:});
> >
> > whos A
> >   Name        Size              Bytes  Class     Attributes
> >   A         888x1776            28424  double    sparse
> >
> > John
> ----------------------------------------
> Thanks a lot John!!
> How about I still want to use the same vector B[1 0] creates this D matrix as below:
> D=
> [1 0 0 0 0 0 0 0;
>  0 1 0 0 0 0 0 0;
>  0 0 0 0 1 0 0 0;
>  0 0 0 0 0 1 0 0];
>
> I don't think we can use the blkdiag function again. Any idea?
---------
I have the solution for the above question but it is not practical for larger matrix.

D=eye(16);
dToRemove = [3,4,7,8,11,12,15,16];
D(dToRemove,:) = [];

Let say the D matrix has bigger size, eye(200) and I don't want to repeat listing all the entries to be remove as above. We can see the pattern of listing from dToRemove variable. Any better suggestion? Thanks in advance.
```
 0

```"sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6764\$lvb\$1@fred.mathworks.com>...

> How about I still want to use the same vector B[1 0] creates this D matrix as below:
> D=
> [1 0 0 0 0 0 0 0;
>  0 1 0 0 0 0 0 0;
>  0 0 0 0 1 0 0 0;
>  0 0 0 0 0 1 0 0];
>
> I don't think we can use the blkdiag function again. Any idea?

You need to define how that vector B will generate that
array. The crystal ball is cloudy, making it impossible

John
```
 0

```"John D'Errico" <woodchips@rochester.rr.com> wrote in message <hs6evg\$5j0\$1@fred.mathworks.com>...
> "sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6764\$lvb\$1@fred.mathworks.com>...
>
> > How about I still want to use the same vector B[1 0] creates this D matrix as below:
> > D=
> > [1 0 0 0 0 0 0 0;
> >  0 1 0 0 0 0 0 0;
> >  0 0 0 0 1 0 0 0;
> >  0 0 0 0 0 1 0 0];
> >
> > I don't think we can use the blkdiag function again. Any idea?
>
> You need to define how that vector B will generate that
> array. The crystal ball is cloudy, making it impossible
>
> John
----------------------------------------
Sorry to confuse you. Lets forget about the vector B and I already constructed that matrix D as I mentioned above but how can I improve it without listing all the columns that I want to delete?

D=eye(16);
rowToRemove = [3,4,7,8,11,12,15,16];
D(rowToRemove,:) = [];
D=[4x8]; %output

Let say the D matrix has bigger size, eye(200) and I don't want to repeat listing all the entries to be remove. We can see the pattern of listing from dToRemove variable. Any better suggestion? Thanks in advance.
```
 0

```"sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6fpo\$qnp\$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hs6evg\$5j0\$1@fred.mathworks.com>...
> > "sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6764\$lvb\$1@fred.mathworks.com>...
> >
> > > How about I still want to use the same vector B[1 0] creates this D matrix as below:
> > > D=
> > > [1 0 0 0 0 0 0 0;
> > >  0 1 0 0 0 0 0 0;
> > >  0 0 0 0 1 0 0 0;
> > >  0 0 0 0 0 1 0 0];
> > >
> > > I don't think we can use the blkdiag function again. Any idea?
> >
> > You need to define how that vector B will generate that
> > array. The crystal ball is cloudy, making it impossible
> >
> > John
> ----------------------------------------
> Sorry to confuse you. Lets forget about the vector B and I already constructed that matrix D as I mentioned above but how can I improve it without listing all the columns that I want to delete?
>
> D=eye(16);
> rowToRemove = [3,4,7,8,11,12,15,16];
> D(rowToRemove,:) = [];
> D=[4x8]; %output
>
> Let say the D matrix has bigger size, eye(200) and I don't want to repeat listing all the entries to be remove. We can see the pattern of listing from dToRemove variable. Any better suggestion? Thanks in advance.

I don't see it as difficult to remove the unwanted
columns. Do it in exactly those two steps. By
trying to combine it into one, you create a problem
that is more complex than it needs to be. While
you COULD do it all with an appropriate call to
sparse, constructing the arguments to sparse will
take more work than just doing it the direct way.

You have two things to be done. Do them one at a
time.

John
```
 0

```"sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6fpo\$qnp\$1@fred.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <hs6evg\$5j0\$1@fred.mathworks.com>...
> > "sarrah " <gto_girlz83@yahoo.com> wrote in message <hs6764\$lvb\$1@fred.mathworks.com>...
> >
> > > How about I still want to use the same vector B[1 0] creates this D matrix as below:
> > > D=
> > > [1 0 0 0 0 0 0 0;
> > >  0 1 0 0 0 0 0 0;
> > >  0 0 0 0 1 0 0 0;
> > >  0 0 0 0 0 1 0 0];
> > >
> > > I don't think we can use the blkdiag function again. Any idea?
> >
> > You need to define how that vector B will generate that
> > array. The crystal ball is cloudy, making it impossible
> >
> > John
> ----------------------------------------
> Sorry to confuse you. Lets forget about the vector B and I already constructed that matrix D as I mentioned above but how can I improve it without listing all the columns that I want to delete?
>
> D=eye(16);
> rowToRemove = [3,4,7,8,11,12,15,16];
> D(rowToRemove,:) = [];
> D=[4x8]; %output
>
> Let say the D matrix has bigger size, eye(200) and I don't want to repeat listing all the entries to be remove. We can see the pattern of listing from dToRemove variable. Any better suggestion? Thanks in advance.

Suppose I wanted to paint a picture. Perhaps a
still life, with red apples, oranges, a banana. I have
paint of various colors. I might try to paint it entirely
in one step, with one large brush, arranging some
of the bristles of my brush to hold different paint
pigments. Now were I truly, incredibly good at this,
I might attempt to paint the entire canvas in one
swipe of the brush.

You recognize of course that the above scheme is
surely impossible. Instead, painting my canvas using
one color at a time seems far more logical.

Solve one problem at a time.

John
```
 0

```"sarrah " <gto_girlz83@yahoo.com> wrote in message
news:hs43m5\$47l\$1@fred.mathworks.com...
> Hi,
> Let say I have a vector B=[1 0] and I want to use this vector B repeating
> in this matrix A having size [8x16].
>
> First, I initialize the matrix A with all zeros then I want to have the
> outcome as below:

*snip*

> How can I use that vector B repeat several times on matrix A using some
> code? It looks like an eye matrix but the pattern is little bit different.
> I tried to do some looping but still doesn't get it as the above.

kron(eye(8), [1 0])

> Second question: I still want to use vector B to create another matrix C
> with size [4x8] as below and how to call it without constructing it
> manually?

I don't see the pattern in C, so I don't know how if at all you'll able to
construct it other than manually.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

```
 0

```"Steven Lord" <slord@mathworks.com> wrote in message <hs7trc\$ssm\$1@fred.mathworks.com>...
>
> "sarrah " <gto_girlz83@yahoo.com> wrote in message
> news:hs43m5\$47l\$1@fred.mathworks.com...
> > Hi,
> > Let say I have a vector B=[1 0] and I want to use this vector B repeating
> > in this matrix A having size [8x16].
> >
> > First, I initialize the matrix A with all zeros then I want to have the
> > outcome as below:
>
> *snip*
>
> > How can I use that vector B repeat several times on matrix A using some
> > code? It looks like an eye matrix but the pattern is little bit different.
> > I tried to do some looping but still doesn't get it as the above.
>
> kron(eye(8), [1 0])
>
> > Second question: I still want to use vector B to create another matrix C
> > with size [4x8] as below and how to call it without constructing it
> > manually?
>
> I don't see the pattern in C, so I don't know how if at all you'll able to
> construct it other than manually.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
The kron function is also flexible for any size and I don't even know the existing of that function. Thanks Steve.

The vector B cannot be used for implementing the matrix C and I already found the correct vector to replicate it in order to get the matrix C.
```
 0