block version of diag function

Søren Hauberg soren at hauberg.org
Thu Mar 6 11:06:06 CST 2008


tor, 06 03 2008 kl. 11:24 -0500, skrev Kamaraju S Kusumanchi:
> 1) If I have a matrix such as
> 
> octave:1> A = [1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
> A =
> 
>     1    2    3    4
>     5    6    7    8
>     9   10   11   12
>    13   14   15   16
> 
> 
> diag(A) gives the diagonal elements of A. Is there any function in octave
> which will give the block diagonals of specified size.
> 
> In the above example diag(A) gives
> 
> octave:2> diag(A)
> ans =
> 
>     1
>     6
>    11
>    16
> 
> 
> I am looking for something that will output
> 
>  1  2  0  0
>  5  6  0  0
>  0  0 11 12
>  0  0 15 16
There might be a simpler way of doing this, but if you use the 'kron'
function, you can do something like this (untested code)
  idx = kron(eye(2), ones(2));
  A.*idx
and if you want the elements
  A(idx)

Søren



More information about the Help-octave mailing list